json POST:在一个 url 本身中发送一个 post 请求

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16229852/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 19:42:38  来源:igfitidea点击:

POST: sending a post request in a url itself

jsonhttpurlpost

提问by Chandeep

I have been given a url .. www.abc.com/detailsand 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:

我得到了一个 url ..www.abc.com/details并要求使用..在这个 url 上发送我的姓名和电话号码POST。他们告诉我使用以下键将内容类型设置为 application/json 并将正文设置为有效的 JSON:

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 javato send the same?

还是我必须用它java来发送?

Please help

请帮忙

回答by Joshua

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.

根据您提供的内容,您需要做的事情非常简单,您甚至可以通过多种方式进行操作。您将需要一些可以让您随请求发布正文的内容。几乎任何编程语言以及像 cURL 这样的命令行工具都可以做到这一点。

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

一个你决定了你的工具,你需要创建你的 JSON 主体并将它提交到服务器。

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

使用 cURL 的示例是(全部在一行中,减去\第一行末尾的):

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"}

回答by Sir l33tname

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

您可以使用 JavaScript 和 Jquery 将数据发布到 url,如下所示:

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

回答by Brindha

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.

不可能以一种直截了当的方式在 url 中发送 POST 参数。POST 请求本身意味着在正文中发送信息。

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.

我找到了一个相当简单的方法来做到这一点。使用 Postman by Google,它允许您将内容类型(标题字段)指定为 application/json,然后提供名称-值对作为参数。

You can find clear directions at http://docs.brightcove.com/en/video-cloud/player-management/guides/postman.html

您可以在http://docs.brightcove.com/en/video-cloud/player-management/guides/postman.html 上找到明确的方向

Just use your url in the place of theirs.

只需使用您的网址代替他们的网址。

Hope it helps

希望能帮助到你

回答by Jayani Sumudini

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...

在Windows中,此命令对我不起作用..我尝试了以下命令并且它有效..使用此命令我在couchdb同步网关中为特定用户创建了会话......

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

回答by Richa Agrawal

You can use postman.

您可以使用邮递员。

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

其中选择 Post 作为方法。并在请求正文中发送 JSON 对象。

回答by rohan

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)youhave 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

如果您通过 url 从浏览器发送请求(如使用 webservice)而不使用 html 页面默认情况下它将是 GET,因为 GET 没有/不需要正文。如果您想将 url 设为 POST,您需要 html/jsp 页面,并且您必须在表单标签中提及“method=post”,因为出于安全原因,post 将包含正文,并且数据将在该正文中传输。所以你需要一个媒介(比如 html 页面)来发出 POST 请求。您不能手动将 URL 设为 POST,除非您通过某种媒介将其指定为 POST。例如,在 URL ( http://example.com/details?name=john&phonenumber=445566)中,您附加了数据(姓名,电话号码),因此服务器会将其识别为 GET 数据,因为服务器正在通过 URL 接收数据,但不是在请求正文中

回答by JDGuide

In Java you can use GETwhich shows requested data on URL.But POSTmethod cannot , because POSThas body but GETdonot have body.

在 Java 中,您可以使用GETwhich 在 URL 上显示请求的数据。但是POST方法不能,因为POST有正文但GET没有正文。