29

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

Chandeep
  • 2,366
  • 10
  • 44
  • 65
  • 1
    From where do you have to send this request? An application, a webpage? In what language? – CodeCaster Apr 26 '13 at 06:20
  • Which programming you are trying to implemented ? – gks Apr 26 '13 at 06: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? – Chandeep Apr 26 '13 at 06: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. – Mysteryos Apr 26 '13 at 06:30

7 Answers7

40

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.

One 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.abc.com/details

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

POST /details HTTP/1.1
Host: www.abc.com
Content-Type: application/json
Content-Length: 44

{"name":"your name","phonenumber":"111-111"}
Joshua
  • 7,792
  • 3
  • 31
  • 39
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"})
});
Sir l33tname
  • 3,329
  • 4
  • 33
  • 43
  • 1
    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 – Ivan Vashchenko Nov 15 '13 at 22:10
5

It is not possible to send POST parameters in the url in a starightforward 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.

Hope it helps

Alexx Roche
  • 2,654
  • 1
  • 29
  • 38
Brindha
  • 329
  • 4
  • 16
  • Link is now broken: Legacy app at https://chrome.google.com/webstore/detail/postman/fhbjgbiflinjbdggehcddcbncdddomop but you probably want: https://chrome.google.com/webstore/detail/tabbed-postman-rest-clien/coohjcphdfgbiolnekdpbcijmhambjff [caveat emptor: I have not verified either app] – Alexx Roche Sep 04 '20 at 14:28
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
Jayani Sumudini
  • 1,164
  • 1
  • 16
  • 26
1

You can use postman.

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

1

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

rohan
  • 133
  • 1
  • 7
-3

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.

JDGuide
  • 5,759
  • 11
  • 39
  • 53
  • 3
    You _can_ POST to an URL that has GET parameters (the `query`), and a GET [can](http://stackoverflow.com/questions/978061/http-get-with-request-body) have a body. Anyway this doesn't answer the question. – CodeCaster Apr 26 '13 at 07:53
  • @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. – JDGuide Apr 27 '13 at 09:02
  • But OP is not asking on advice whether to use POST or GET, the question is _how to make a POST request_. – CodeCaster Apr 27 '13 at 09:36