1

When I send a post request to this api in postman I get a response output to my post request. I just want to know how I can print this to the screen and/or parse it to use the response for further use

url = ("https://mysupersecretapi/123456/execute")

    headers =  {'Accept': 'application/json', 'Accept-Encoding': 'gzip, deflate', 'Authorization': 'basic' , 'Content-Type':'application/json;charset=utf-8'}
    perameters = {
"inputs": {
"Container_Full": 'true',
"Container_Note": "",
"Output_Stream_Id": "1",
"Piece_Serialized": "<Container><Pieces><PieceSerialNo>"+strContainer[0]+"</PieceSerialNo> <CustomerSerialNo/><Note></Note></Pieces></Container> ",
"Qty_Is_Container_Qty": 'true',
"Quantity": 1,
"Workcenter_Key": 58368,
"Validate_Only": 'false',
"Start_New_Container": 'true'
  }
}

    plexReq = requests.post(url,auth=HTTPBasicAuth, headers=headers, json=perameters)
    print(plexReq.request.headers)
    ####START HERE

    print(plexReq.status_code)
    response = requests.get(url)
    plexResponse = json.dumps(response.json)
    print(plexResponse)

'''

HERE IS THE OUTPUT I GET WHEN USING POSTMAN

{
"outputs": {
    "New_Container": null,
    "New_Serial_No": null,
    "Recorded_Master_Unit_No": null,
    "Recorded_Part_No": null,
    "Recorded_Quantity": null,
    "Recorded_Revision": null,
    "Recorded_Serial_No": null,
    "Result_Code": 5106,
    "Result_Error": true,
    "Result_Message": "Workcenter status does not allow production",
    "Validation_Failed": false
     },
     "tables": [],
    "transactionNo": "2069710"
     }

'''

1 Answer 1

1

Not sure what you mean by Printing to the screen?. PostMan is simply an API tool for making API requests and it's not a Python interpreter in order to be able to print it to the screen.

To parse it to use the response for further use? There is no way to parse fetched data to be used for another request. It could only be done manually in the request URL.

Edit: this only applies to PostMan and can be solved if your requests are from your application, see here on that.

Sign up to request clarification or add additional context in comments.

5 Comments

Btw, this only applies to PostMan and can be solved if your requests are from your application, see here on that.
So when i use the post method on that API, which is a stored procedure that records production, after the procedure executes it returns an output response containing the serial number of the container if the procedure executed properly. When i use postman it gives that output response i showed. I want to do the same in python
Consider using a library like urllib2 see here. Make the necessary call to the URL and it will give you the response as expected. You may also want to check the API docs of the site that you are requesting as they may also have relevant code snippets that may be of help to you.
I just checked out that link in your first comment for postman. I didnt realize they generated code snippets. That helps a lot, turns out i needed to use requests.request(“POST”, url, headers=headers, data = payload) and then print the output using print(response.text.encode(‘utf8’)) Thanks for your help I appreciate it!
@Damian_W If this answer helped in your problem, please mark it as accepted by clicking the check mark next to the answer. see here for more informatio

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.