I know how to post json and I know how to post file in multipart form in Postman. But how can I do both together. For example:
Here is my jason to post:
{
"title": "Post title yeah",
"body": "My first post body"
}
So how can I post image.jpg located at /home/me/Desktop along with the the above json?
UPDATE: Note that I want so send file using JSON, so my question is different from this which is about multipart form sending using Postman.
You can use this online tool to convert your file to a base64 (https://www.browserling.com/tools/file-to-base64), then you can send it as part of the json. I reduced the size of the base64 string for the answer.
{
"title": "Post title yeah",
"body": "My first post body",
"image": "JVBERi0xLjQKJeLjz9MKMiAwIG9iago8PC9MZW5ndGggMjcyNS9GaWx0ZXIvRmxhdGVEZWNvZGU+PnN0cmVhbQpYCb1c247byBF9X8D/0MC+JIFNs5t9IY0gwHicXRjI2GOPvPsQ5EGrocdKRGmiofby9+kmu5t901BFKoLhi8qqc04Xq07zZr/47r8olz84JlmORKV+3dfoZ7R9If8GcxVkeUZLjijNBEOYZhgT+523C/T6hwJVWVVVaPG1w5IID4gxluESLf6BMMuqkkqYImOMoMU9+tPH/X29R9+/+TNa/Bst/oL+vnjx3aeBkJcZqwQilfwwnU/gjFe843u3bOuYjHcLUstjVYlwgfX6cp+NJtgoI5no6RQKKSWMTGQ9HaG4EDzio2o9QshqckkjMklGMsJAbAqjIBKlyHJOO7acvCb8NcnlFzB7g+kbUqbrykUla0kR491CeVZFdcV4vLC8JFlJy477bre5R4v"
}
Related
I have a backend that will respond to a mobile frontend like this:
{
"id": 12345,
"url": "https://www.example.com?origin=Papá"
}
The problem is, the param origin in the URL has an accentuation mark, so in iOS the parser breaks and the link does not work.
So what I thought about doing was to encode the entire URL. So my response would look like this:
{
"id": 12345,
"url": "https%3A%2F%2Fwww.example.com%3Forigin%3DPap%C3%A1"
}
Is this correct? Should I do this? What is the right way to go about sending URLs in a JSON response?
I have an API that I am posting to that accepts posts from me in Postman if the body is form-data and returns a 201. If, however, I format the body as JSON, I get back a 400 response indicating all the missing fields - e.g.
"errors": [
{
"code": "1000",
"message": "Missing parameter",
"origin": "first_name"
},...etc
I do not get, however, any indication that JSON is not accepted. Is there a way that I can determine if the API accepts posts with a JSON Body? If it does, it this the response I should be getting?
I am trying to upload a text file to OneDrive using Graph APIs and I also want to update it in the same request using JSON batch.
My JSON request body is below:
{
"requests":[
{
"id":"1",
"method":"PUT",
"url":"/drives/b!ddubdQaackeT9nu3x4onivgPxHH2-
gpFsk_mo9hryZabqK7w279YSpMqiNodZDaa/items/01BTTSDZ56Y2GOVW772
5BZO354PWSELRRZ:/abc2.txt:/content",
"headers":{
"Content-Type":"application/octet-stream",
"Content-Length":"21"
},
"body":{
"content":"Test content for body"
}
},
{
"id":"2",
"method":"PATCH",
"url":"/drives/b!ddubdQaackeT9nu3x4onivgPxHH2-
gpFsk_mo9hryZabqK7w279YSpMqiNodZDaa/items/01BTTSDZ56Y
2GOVW7725BZO354PWSELRRZ:/abc2.txt",
"headers":{
"Content-Type":"application/json; charset=utf-8"
},
"body":{
"fileSystemInfo":{
"lastModifiedDateTime":"2020-08-09T00:49:37.7758742+03:00"
}
},
"dependsOn":["1"]
}
]
}
When I send this request from my code, I always get a response "Invalid body for request id: 1. The body must be a valid base64 string or JSON.".
Postman refused to run the above request with the message "Method not allowed".
In the above example, I am uploading and updating text files but my code will have to handle all file types (e.g. images, videos, etc.)
Not sure if I am correctly specifying all the JSON fields. Unfortunately unable to find much info on this. Any help would be appreciated.
I have a JSON response as follows,
{
"name":"John Smith",
"address": "#123\r\nRenault Road\r\n123456\r\n"
}
and I need to extract the address and put it into my next POST request. I use the JSON Extractor as a post processing element and with $..address I set the extracted address to the variable address.
In my next request, I use the following as the POST data,
{
"id": "123456",
"address": "${address}"
}
So when I make the request I see the POST data as,
{
"id": "123456",
"address": "#123
Renault Road
123456
"
}
This breaks at my backend and nevertheless this payload is not identified as a valid JSON as well.
I want to make the request so that the POST data is as follow,
{
"id": "123456",
"address": "#123\r\nRenault Road\r\n123456\r\n"
}
Any help to get this done is highly appreciated.
If you really need to send these line breaks as \r\n you can use some scripting to convert them back.
Add Beanshell PreProcessor as a child of the "next request" HTTP Request Sampler
Put the following code into the PreProcessor's "Script" area:
address = vars.get("address");
address = address.replaceAll("\\r\\n","\\\\r\\\\n");
vars.put("address", address);
The above script will convert line breaks to the textual representation.
References:
vars is a shorthand for JMeterVariables class instance, it provides read/write access to all JMeter Variables
String.replaceAll() - is the method of String class which comes out of box with Java SDK
How to Use BeanShell: JMeter's Favorite Built-in Component - guide demonstrating how to use Java and JMeter APIs using Beanshell test elements to enhance your JMeter tests with scripting if required
I want to upload a json file to my cassandra DB by using a Play application/api.
My html view looks like this.
#main("Welcome to Play") {
<h1> Upload a JSON file </h1>
#helper.form(action = routes.Application.upload, 'enctype -> "text/json") {
<input type="file" name="jsonFile">
<p>
<input type="submit" value="Upload">
</p>
}}
I defined my upload method in the controller:
def upload = Action(parse.json) { request =>
val data = Json.arr(request.body)
sc.parallelize(Seq(data)).saveToCassandra("person", "user", SomeColumns("name", "age"))
Redirect("/index") }
The error message I get is
Bad Request: For request 'POST /upload' [Expecting text/json or application/json body]
Tried with enctype "application/json" as well and there was no difference.
If I try to print the content out I get Null.
The format of my file.json is:
{ "name": "alice", "age": 22}
{ "name": "bob", "age": 23}
Basically I can't seem to properly extract the content from the file.
See this section of the docs, about request body parsers:
https://www.playframework.com/documentation/2.5.x/ScalaBodyParsers#Choosing-an-explicit-body-parser
Basically, parse.json requires that you made a request with Content-Type of application/json. From the docs:
The json body parser will validate that the request has a Content-Type of application/json, and send back a 415 Unsupported Media Type response if the request doesn’t meet that expectation. Hence we don’t need to check again in our action code.
There is also another body parser that does not have this requirement. Again, from the docs:
This of course means that clients have to be well behaved, sending the correct Content-Type headers with their requests. If you want to be a little more relaxed, you can instead use tolerantJson, which will ignore the Content-Type and try to parse the body as json regardless:
def save = Action(parse.tolerantJson) { request =>
Ok("Got: " + (request.body \ "name").as[String])
}
So, you have two options here:
Use parse.json and set Content-Type to application/json
Use parse.tolerantJson
Besides that, you JSON file is not valid if the content is like this:
{ "name": "alice", "age": 22}
{ "name": "bob", "age": 23}
You can check it using JSONLint and I also recommend you to read the definition at json.org. You probably want to have a array like this:
[
{ "name": "alice", "age": 22},
{ "name": "bob", "age": 23}
]
After correct the format, you can either use parse.json or parse.tolerantJson.