简体   繁体   English

从 Bash 脚本 POST 获取 Json 正文和响应状态

[英]Get Json body and Response Status from Bash Script POST

Currently I am using:目前我正在使用:

#!/bin/bash

PROCESS=$(curl --location --request -v -X POST 'https://jsonplaceholder.typicode.com/posts' \
--header 'Content-Type: application/json' \
--data-raw '{"title": "foo","body": "bar","userId": "1"}')

echo "$PROCESS"

And getting:并得到:

  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100   111  100    67  100    44    208    137 --:--:-- --:--:-- --:--:--   344
{
  "title": "foo",
  "body": "bar",
  "userId": "1",
  "id": 101
}

But I also want the response status eg 201 or like this.但我也想要响应状态,例如 201 或类似的。

HTTP/2 200 
date: Mon, 30 Nov 2020 14:00:56 GMT
content-type: application/json; charset=utf-8
set-cookie: __cfduid=dfda1e85d5738eb18115dc0a07311a4dd1606744856; expires=Wed, 30-Dec-20 14:00:56 GMT; path=/; domain=.typicode.com; HttpOnly; SameSite=Lax
x-powered-by: Express
x-ratelimit-limit: 1000
x-ratelimit-remaining: 999
x-ratelimit-reset: 1606702897
vary: Origin, Accept-Encoding
access-control-allow-credentials: true
cache-control: max-age=43200
pragma: no-cache
expires: -1
x-content-type-options: nosniff
etag: W/"6b80-Ybsq/K6GwwqrYkAsFxqDXGC7DoM"
via: 1.1 vegur
cf-cache-status: HIT
age: 13185
cf-request-id: 06bb0df15c0000edfbfb9b8000000001
expect-ct: max-age=604800, report-uri="https://report-uri.cloudflare.com/cdn-cgi/beacon/expect-ct"
report-to: {"endpoints":[{"url":"https:\/\/a.nel.cloudflare.com\/report?s=ABBCY6aKAHfezboFKgcq%2FlsWKQZDAORup49fKMArhm%2BYl3Kb99pMLrZpLtbXsfz%2BQ6RxnutmzE0mCX5AcIVGRjmq%2FIrIja5MeNFFnmpO7WBT1725PWdN1J0KFhcqNxvNP8He2TBjfd3N"}],"group":"cf-nel","max_age":604800}
nel: {"report_to":"cf-nel","max_age":604800}
server: cloudflare
cf-ray: 5fa518fbcbdfedfb-CDG

I want to do the post and the echo out body and response code in a nice way.我想以一种很好的方式发布帖子并回显正文和响应代码。

Response code is sent in HTTP headers.响应代码在 HTTP 标头中发送。 You may redirect headers to STDERR eg as described here: Report HTTP Response Headers to stderr?您可以将标头重定向到 STDERR,例如如下所述: Report HTTP Response Headers to stderr?

So you may do this:所以你可以这样做:

out=$(curl -s -D /dev/stderr http://boardreader.com 2>/tmp/headers)
# parse /tmp/headers

If you don't want to mess with temp file, you may try more complex solutions like Capture stdout and stderr into different variables如果您不想弄乱临时文件,您可以尝试更复杂的解决方案,例如将标准输出和标准错误捕获到不同的变量

You can only issue either a post or header request in one call and so you will need to do this in two separate calls read into the same variable and so:您只能在一次调用中发出帖子或 header 请求,因此您需要在读取同一变量的两个单独调用中执行此操作,因此:

PROCESS=$(curl -I 'https://jsonplaceholder.typicode.com/posts' && curl -X POST 'https://jsonplaceholder.typicode.com/posts' --header 'Content-Type: application/json' --data '{"title": "foo","body": "bar","userId": "1"}')

To me it makes sense to check the headers first and if this command is successful, get the json response with both being read into the PROCESS variable.对我来说,首先检查标头是有意义的,如果此命令成功,则获取 json 响应,并将两者都读入 PROCESS 变量。 You can of course change the order if you wish.如果您愿意,当然可以更改顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM