How to stimulate cURL request to a request using postman

前端 未结 2 1201
北恋
北恋 2020-12-30 05:23

I am using the following cURL request to localhost which runs fine:

curl -u admin:e4d4face52f2e3dc22b43b2145ed7c58ce66e26b384d73592c -d \"{\\\"jsonrpc\\\": \         


        
相关标签:
2条回答
  • 2020-12-30 05:36

    Curl is using HTTP Basic authentication by default. Your headers set in Postman are something different. Try using Basic Auth in Postman. It is in top panel, you fill in username and password and authorization header will be generated.

    0 讨论(0)
  • 2020-12-30 05:54

    When using cURL, the -u option (or --user) is used to supply the credentials for HTTP Basic authentication. This sets the Authorization header to contain the necessary data to authenticate with the server.

    These steps apply to Postman's packaged app. For steps for the legacy app, view this of revision this answer.

    To use HTTP Basic authentication as you were in your cURL command, click the Authorization tab and enter your credentials. Clicking Update Request will add the necessary Authorization header for you.


    Postman's Authorization tab


    Postman's Headers tab


    To submit the JSON data in the same way that you did with cURL, use a POST request, select raw under the Body tab, and enter your data like so:


    Postman's Body tab


    To debug this I used Fiddler - a free web debugging proxy.

    I used cURL's --proxy option to make it send its requests through Fiddler like so:

    curl \
    --proxy http://localhost:8888 \
    -u foo:bar \
    -d "{\"jsonrpc\": \"2.0\", \"method\": \"feed.list\", \"id\": 1}" \
    http://localhost
    

    Now that the request goes through Fiddler, I can select it from the session list, and use the "raw" inspector to see the raw request:


    Fiddler's "raw" inspector


    This shows me that the cURL is making a POST request with HTTP Basic authentication and application/x-www-form-urlencoded content. This type of data normally consists of keys and values, such as foo=bar&hoge=fuga. However, this cURL request is submitting a key without a value. A call to var_dump($_POST) will yield the following:


    var_dump output


    With a = at the end of the data (like so: {"jsonrpc": "2.0", "method": "feed.list", "id": 1}=) the var_dump will yield the following:


    var_dump output


    However, it seems that JsonRPC will use file_get_contents('php://input') in your case. This returns the data that was submitted with the request, including a =, if the data ends with it. Because it will try to parse the input data as a JSON string, it will fail if the string ends with a =, because that would be invalid JSON.

    Using the FoxyProxy extension for Chrome, I created a proxy configuration for Fiddler (127.0.0.1:8888), which allowed me to easily debug the data being sent by Postman's POST request. Using x-www-form-urlencoded with a key of foo with no value, the data sent was actually foo=, which would result in your JSON string being invalid.

    However, using "raw" input will allow for the specified data to be sent without a = being added to the end of it, thus ensuring the data is valid JSON.

    0 讨论(0)
提交回复
热议问题