How to pass json in testing APIs using pytest - pytest

In falcon 1.1.0, the only way to send data is through body which takes byte data. How can we post json in such a situation using the simulate_post method while testing falcon APIs using pytest.

Use body to send JSON as a string:
data = json.dumps(data)
client.simulate_request(method='POST', path=url, body=data)
Optionally you can also set the content-type header to indicate it's a JSON request:
headers = {'Content-Type': 'application/json'}
data = json.dumps(data)
client.simulate_request(method='POST', path=url, headers=headers, body=data)

Related

How to read Gzipped payload in a POST request in SpringBoot

I need to read gzipped json payload in a POST request in my SPringBoot app which accepts json data. How to do that in order to keep the application generic as there may be other clients in future sending data in plain json or other compression formats? I suppose this should be handled by the server itself so is there any way to instruct the embedded Tomcat to unzip the payload?
My SpringBoot application runs on embedded Tomcat 9.0.17.
The controller accepts JSON payload in a POST request.
#RequestMapping(value = "/update/v1", method = RequestMethod.POST, produces = "application/json", consumes = "application/json")
public ResponseEntity<String> receiveUpdates(#RequestBody String update) {
We recently changed our content provider and the new one is sending payload in "gzip" format (with header content-encoding=gzip) and without any content-type header. As a result it gives the following error
'error': 'Unsupported Media Type', 'message': "Content type '' not supported"
If I change my consume type to MediaType.ALL_VALUE, my controller starts receiving the request but the payload itself is gzipped. I can handle it in my service layer but that would make it specific to gzipped data.
This problem could be solved by introducing a Filter to handle gzipped payload as mentioned here and here.
But I believe there should be a way to instruct the Tomcat to handle this and serve unzipped data.

Trying to do some assertion from request, which will be present in response

I am passing the request as in my feature file and i am trying to do assert from request to response.
I have tried must contains queries but i am not sure if i am doing it correct, could you please help.
**Background:**
* configure headers = read('classpath:merchantServiceheaders.js')
Given url MservUrl
And path '/spapis/rest/sp-ms-engine/sp/ms/v1/engine/scanandredact'
Scenario Outline: ACH Low Value Payment Rips Services Summary
]
}***
What i would like to do is assert what i have in my request to what i will get back in response.
Since i am passing subject in request the same subject should be present in response
Possible in 0.9.3: https://github.com/intuit/karate#scenario-outline-enhancements
First change the Examples: column header to data!
And request data
When method post
Then status 200
And match response contains data
In 0.9.2 and earlier, with the Examples: column header as data
* def data = <data>
And request data
When method post
Then status 200
And match response contains data

How to set Content-Type in swift3 alamofire

I am sending data in JSON with content-type JSON but this shows me the content-type XML.
So the server could not read my request.
Also, that is the post request
The problem is that you are using .responseJSON which tells Alamofire that the response would contain JSON. Since the response is XML and not JSON in your case, Alamofire would throw an error. What you need instead is not to specify the response type and then an XMLParser to parse the data. One option is SWXMLHash. Your updated code would look something like this
Alamofire.request(request)
.response { response in
var xml = SWXMLHash.parse(response.data!)
}
Basically, this is not an error of content-type. this is an error of data type.
I send the all value in the string but there is need to send the data with data type.
When I request with data type it automatically changed content-type in JSON.

Accepting api keys in HTTP headers or JSON POST data

I have an HTTP JSON endpoint localhost:8000/resource/ that takes JSON data as input and returns JSON as output. I am now adding API Key based authorization to the endpoint. There are 2 ways to accept the API Key at this endpoint:
Method A: In the request headers
Example python code:
import requests
headers = {
'API-Key': '<my-api-key>',
}
r = requests.post('http://localhost:8000/resource/',
json={'input': <value>},
headers=headers)
Method B: In the JSON data itself
Example python code:
import requests
r = requests.post('http://localhost:8000/resource/',
json={'input': <value>, 'API-Key': '<my-api-key>'},)
I usually notice Method A being adopted. Is there anything wrong with latter approach in which the API key is passed along with the other json data?
I think it has to do with clarity, the API Key isn't really relevant to the input, it's just a form of authorization.
Large frameworks that deal with routing and such are able to filter based on specific headers, and it would be cumbersome to filter based off of a specific form of input in the request body that would require user to intervene and obtain that value for it. Headers are simpler, and suffice for simple data that would fit in a hash-table.

eve framework and HMAC auth

I'm computing the HMAC of the HTTP body request in this way:
payload = {"name":"myvm","os":"gentoo","resources":{"vCPU":"4","RAM":"512","Disk":"1000"},"actions":["start"]}
key = "supersecretkey"
secret = bytes(key, encoding='utf-8')
msg = json.dumps(payload, sort_keys=True)
message = bytes(msg, encoding='utf-8')
print(hmac.new(secret, message, sha1).hexdigest())
After that I encode with b64 the hexdigest and send it with curl like the docs says Authorization: paolo:$hmac_base64_encoded
The problem is that I always get a 500 error. What am I doing wrong?
Since you are using Python 3x make sure that in your custom HMACAuth you are converting to bytes (the code snippet from the official documentation is for Python 2x).