POST image via form-data error - JSON_SYNTAX_EXCEPTION

Hi All,

POSTMAN - Version 10.12.0
I am trying to call a REST API via POST and I need to pass an image as a parameter. I tried form-data to pass parameters but getting following error.
{
“error”: {
“code”: “REQUEST_ERROR”,
“message”: “Malformed Request.”,
“details”: [
{
“code”: “JSON_SYNTAX_EXCEPTION”,
“message”: “The syntax of the JSON document is not valid.”
}
]
}
}

Any clues as to why I get this error?

Best regards
Gayan

This will be down to what the API will accept.

The error is telling you that the request is formatted incorrectly.

How it should be formatted will be down to the API specification.

Can you share the API documentation?

Hi Mike,

I can send the payload except the image as raw/json and it works.

Spec:
Payload
{

  • “ItemId”: 1,

  • “Name”: “It is a Text”,

  • “Description”: “It is a Text”,

  • “Obsolete”: true,

  • “MediaFile”: “It is a Text”,

  • “MediaItemType”: “Image”,

  • “PrivateMediaItem”: true,

  • “Repository”: “Database”,

  • “CreatedBy”: “It is a Text”,

  • “MediaObject”: “”,

  • “MediaText”: “”,

  • “MediaItemTypeDb”: “It is a Text”,

  • “LockConnectionCount”: 1,

  • “FndUser”: “It is a Text”,

  • “IsAdmin”: “It is a Text”

}

Best regards
Gayan

Hi Mike,

I found that ‘data’ is the issue and it gives the error basically.
I am not sure how to pass other payload attributes except the image in form-data page.

Best regards
Gayan

There are several media fields in that example, however, all of them are showing a string data type. (As I would expect for a JSON request).

You can’t send a binary file using raw\JSON.

The example isn’t really showing what needs to be added here.

I’m guessing (and its just a guess) that the media file needs to be added as a base64 encoded string as you can’t send binary data outside of form data. MediaText allows 64000 characters (as the other fields would be too small for a base64 string).

Need much more information on the API. That request example is pretty poor.

Hi Mike,

I believe the problem is not with the image. I found that even if you add a single attribute/value pair to the form-data, you get the same error.
Have gone through lots of forums and tried to send standard text attributes as different ways but getting the same error.

Option 1: as data (note: Data is NOT an attribute in the payload)
key
data

value
{“Name”: “Testing”,“Description”: “My Description”,“MediaItemType”: “Image”,“PrivateMediaItem”: true,“Repository”: “Database”}

Option 2:
As key/value pair in form-data
example:

Key Value
Description Test Description

Still getting same error.

Do you have any instructions for that API that specifically tells you to use form-data?

The screenshot shows that the request body schema is application/json, which means raw\JSON in Postman which you say is working apart from the image.

Therefore I would expect this to cause an error if you try and use form-data.

JSON requests are basically text. Therefore I’m not expecting you to be able to attach a binary file to this request in Postman.

The usual process would be to base64 encode the file (which would create a large text string) and that can be included in the JSON body (but its not obvious from your sample if this API can even do that).

Therefore, can you advise what API is this using?
Can you please link to the documentation if that is possible.

Hi Mike,

I believe this is a very simple payload but for some reason it is not working for even application/json for some reason in this version.
Just forget about the optional image.
form-data should accept application/json.
please refer following link.

I will do my own investigation as to see why form data is not working for a simple json payload.

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.