Invalid JSON message when making Post Request

Hello

I successfully made my first GET request using the API. But when I tried making a past to create a new task, I get the message from the server: “Invalid JSON”.

Now I am confused, the responses from the server come in JSON format, I should be the one saying that if anyone, ha.

My request simply includes a URI as the documentation shows here: Build an app with Asana

/api/1.0/tasks/?assignee=xxxxxxxx?notes=Test%20Task?name=Test%20Task?workspace=xxxxxxxxx

What is this JSON I am supposed to be supplying and where do I put it?

Hey. Try this:

/api/1.0/tasks/?assignee=xxxxxxxx&notes=Test%20Task&name=Test%20Task&workspace=xxxxxxxxx

Ah, I did overlook the & but even with the correct syntax it is still complaining about some json. I do not understand, there is no supplying JSON in a web request unless I wrap it up into the content body or something. I am making a POST request with a URI and an empty content body.

Here is the full error

{“errors”:[{“message”:“Could not parse request data, invalid JSON”,“help”:“For more information on API status codes and how to handle them, read the docs on errors: Build an app with Asana”}]}

How you do the POST request? Are you using an Asana client or by curl?

You can use Postman to do the Json request by providing in request body a valid json format. See how here:

java - How to send JSON Object from POSTMAN to Restful webservices - Stack Overflow service

@Chad_Saar I believe the mistake here is that you’re trying to POST data through URL query parameters, which is incorrect. Data must be provided either as URL-encoded form data or JSON. When the API sees that no form fields are present, it attempts to look for a JSON body, and an empty body is invalid JSON.

I would like to use the HTTP Web Request feature within the proprietary visual scripting application. It is not that I can’t switch over to C# within the application, but I have been using this HTTP Web Request feature with many many REST API’s over the years and it has always worked great. It is also easier to show the customer how to make smaller changes once configured, without them needing to know C#.

So this HTTP Web Request module is very straight forward.
You choose the rest type (GET, POST, etc).
You enter the URL and the URI.
You enter the header information (where the access key goes).
If you choose POST, then you have an option to select the content type and enter a content Body.
It has everything I need to work with REST API’s.

However the documentation mentions nothing about supplying any JSON or body information. Also the explorer seems to be indicating only a URI is needed in all cases, I never see it using a content body. Am I missing some documentation maybe?

Also encoding the URI is no problem. I have a function to apply the encoding.

Ok I am able to send it a JSON content body. However it says right away that assignee is not a valid field, so I don’t think it accepts it in JSON format.

Here are the instructions for creating a task. From what I understand about --data-urlencode, this just encodes the data (IE replaces spaces, etc) and adds it to the URI as ?assignee=1234 etc.

curl -H “Authorization: Bearer <personal_access_token>”
https://app.asana.com/api/1.0/tasks
–data-urlencode “assignee=1235”
–data-urlencode “notes=How are you today?”
–data-urlencode “collaborators[0]=5678”
–data-urlencode “name=Hello, world!”
–data-urlencode “workspace=14916”

Using --data-urlencode does not move the data to the query parameters, but rather sends it in the request body. You can see this by running curl with the --trace-ascii option:

$ curl https://httpbin.org/post \
    --data-urlencode key=value \
    --trace-ascii /dev/stdout
...
=> Send header, 148 bytes (0x94)
0000: POST /post HTTP/1.1
0015: Host: httpbin.org
0028: User-Agent: curl/7.54.0
0041: Accept: */*
004e: Content-Length: 9
0061: Content-Type: application/x-www-form-urlencoded
0092: 
=> Send data, 9 bytes (0x9)
0000: key=value
...

Here, the URI is just /post with no query parameters, and after the headers is the body, which is the data key=value.

The Asana API explorer will allow you to add “extra parameters” which, for POST and PUT requests, are transmitted in the request body. You can see an example of how to send both form data and JSON in our documentation on input/output options.

Thank you I found it in that link under Re-Assing task, then under Alternative method using JSON which shows you need to supply the information in this format.

{
“data”: { “assignee”: 1234 },
“options”: { “expand”: “projects” }
}

I just continued where it left on on assignee with the rest of the parameters listed in the create task article and it works! Whew, I was confused because I do not use the curl library and was trying to reverse-engineer it to find out what data I needed to supply and how.

1 Like