2

I have a URL https://mylink/apply?data= for a server which accepts the data variable as valid JSON of following properties:-

  • name: String with your name (required)
  • email: String with your email address (required)
  • urls: Array of strings with links
  • comment: String with any comment/request you might have (optional)

My JSON file "data.json" is the following:

{
    "data":
    {
        "name": "Rogers Bell",
        "email": "[email protected]",
        "urls":["https://stackoverflow.com/users/3067241/imran", "https://github.com/i4ali"],
        "comment":"none"
    }
}

When I use the following CURL command to send a POST request, I get error code 400 Bad Request with a message from the server Error: no data passed. indicating something wrong with my JSON format or command. Not sure what I am doing wrong here

curl -v -i -X POST -d @data.json https://mylink/apply?data= -H "Accept: application/json" -H "Content-Type: application/json" -k

I am using Windows 7 if that matters

5
  • I suggest you either use GET and send everything in the query, or use POST and put the entire query in the body. But I'd need the API documentation to give you a definite answer... Commented Sep 9, 2017 at 21:09
  • My guess - you need to specify header application/json Commented Sep 9, 2017 at 21:14
  • @Konrads I am doing that already with the -H "Content-Type: application/json" in the CURL command Commented Sep 9, 2017 at 21:15
  • 1
    When JSON file content is sent by curl, all carriage returns and newlines will be stripped out -- it would still be a valid JSON but server may not be able to recognize. Can you send the same content (with carriage returns and newlines) through Postman and check again? Is there any API document? Commented Sep 10, 2017 at 9:29
  • @shaochuancs I tried postman putting the json under body->raw(json) format but I get the same error code '400 Bad Request' and a message 'Error: no data passed'. Unfortunately API is not free to look at Commented Sep 10, 2017 at 19:38

2 Answers 2

5

The API is pretty weird. But if you have to read JSON file and POST its content to server as URL parameter, you can cat the file, encode it and send it in curl. Here is an example:

curl -G -X POST https://requestb.in/1n88lah1 --data-urlencode data="$(cat input.json)"

The input.json would look like:

{
    "name": "Rogers Bell",
    "email": "[email protected]",
    "urls":["https://stackoverflow.com/users/3067241/imran", "https://github.com/i4ali"],
    "comment":"none"
}
Sign up to request clarification or add additional context in comments.

Comments

-1

Using Postman I was able to send this POST request with the following complete URL. Got a 200 OK response

https://mylink/apply?data={
    "name": "Rogers Bell",
    "email": "[email protected]",
    "urls":["https://stackoverflow.com/users/3067241/imran", "https://github.com/i4ali"],
    "comment":"none"
}

3 Comments

Are you sending JSON data through URL parameter? The API is so weird...
yes, exactly. I was also surprised this worked since my understanding was you cant do a POST request with JSON data through query strings
Actually, you can send POST request with parameter in URL. However, normally this is not the way to send data.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.