43

I have been given a url .. www.abc.com/details and asked to send my name and phone number on this url using POST. They have told me to set the content-type as application/json and the body as valid JSON with the following keys:

name: name of the user phone number: phone number of the user 

Now i have no clue how to send this request! Will it be something like:

http://www.abc.com/details?method=post&name=john&phonenumber=445566 

or do i have to use java to send the same?

Please help

4
  • 1
    From where do you have to send this request? An application, a webpage? In what language? Commented Apr 26, 2013 at 6:20
  • Which programming you are trying to implemented ? Commented Apr 26, 2013 at 6:20
  • that is the point .. they have just told that i'll be getting a response of 200 code if success from the server. they haven't told anything else .. can i simply write a url on the browser and submit the same? or do i have to use a programming language? Commented Apr 26, 2013 at 6:26
  • Ideally, u'll have to use html, and jquery coupled with an AJAX request onto a PHP file that sends the data using POST. In the PHP file, you can use CURL request to POST onto your url and retrieve a response. Commented Apr 26, 2013 at 6:30

7 Answers 7

52

Based on what you provided, it is pretty simple for what you need to do and you even have a number of ways to go about doing it. You'll need something that'll let you post a body with your request. Almost any programming language can do this as well as command line tools like cURL.

Once you have your tool decided, you'll need to create your JSON body and submit it to the server.

An example using cURL would be (all in one line, minus the \ at the end of the first line):

curl -v -H "Content-Type: application/json" -X POST \ -d '{"name":"your name","phonenumber":"111-111"}' http://www.example.com/details 

The above command will create a request that should look like the following:

POST /details HTTP/1.1 Host: www.example.com Content-Type: application/json Content-Length: 44 {"name":"your name","phonenumber":"111-111"} 
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks for adding the usage of curl. It helps programmers unfamiliar with REST endpoints see the bare bone mechanisms of how to make one using just a terminal.
I got a similar challenge (though more complex) as part of a job application and the last thing I stumbled on was not having the quotes around the curly brace json data. While my challenge documentation claimed that there would be useful error messages if the POST did not go through, I got a HTTP 400 response with no helpful details.
8

You can post data to a url with JavaScript & Jquery something like that:

$.post("www.abc.com/details", { json_string: JSON.stringify({name:"John", phone number:"+410000000"}) }); 

1 Comment

In my case $.post("details", {name: "John, phone: "555-555-5555"}); worked like a charm. Tip: do it in browser console while visiting www.abc.com
6

It is not possible to send POST parameters in the URL in a straightforward manner. POST request in itself means sending information in the body.

I found a fairly simple way to do this. Use Postman by Google, which allows you to specify the content-type (a header field) as application/json and then provide name-value pairs as parameters.

You can find clear directions at [2020-09-04: broken link - see comment] http://docs.brightcove.com/en/video-cloud/player-management/guides/postman.html

Just use your URL in the place of theirs.

2 Comments

Link is now broken: Legacy app at chrome.google.com/webstore/detail/postman/… but you probably want: chrome.google.com/webstore/detail/tabbed-postman-rest-clien/… [caveat emptor: I have not verified either app]
What if you send all the arguments in the URL, like with a GET request?
3

If you are sending a request through url from browser(like consuming webservice) without using html pages by default it will be GET because GET has/needs no body. if you want to make url as POST you need html/jsp pages and you have to mention in form tag as "method=post" beacause post will have body and data will be transferred in that body for security reasons. So you need a medium (like html page) to make a POST request. You cannot make an URL as POST manually unless you specify it as POST through some medium. For example in URL (http://example.com/details?name=john&phonenumber=445566)you have attached data(name, phone number) so server will identify it as a GET data because server is receiving data is through URL but not inside a request body

Comments

2

You can use postman.

Where select Post as method. and In Request Body send JSON Object.

Comments

1

In windows this command does not work for me..I have tried the following command and it works..using this command I created session in couchdb sync gate way for the specific user...

curl -v -H "Content-Type: application/json" -X POST -d "{ \"name\": \"abc\",\"password\": \"abc123\" }" http://localhost:4984/todo/_session 

Comments

-4

In Java you can use GET which shows requested data on URL.But POST method cannot , because POST has body but GET donot have body.

3 Comments

You can POST to an URL that has GET parameters (the query), and a GET can have a body. Anyway this doesn't answer the question.
@CodeCaster maybe in RESTFUL web service GET can have body,i donot know.But, I am following the book HEAD FIRST - JSP and Servlet,from O'reilly .As per that book, my above statements in answer is correct.It may not the appropriate answer for the posted question.You can see the page no-110 in that book.GET has no body. where POST has message body and this is the key point that POST handle more data and take more parameter than GET.
But OP is not asking on advice whether to use POST or GET, the question is how to make a POST request.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.