How can I get Paw to prompt me to provide a value? E.g. for a name field in a JSON request body such as the following:
{
"name": [prompt me when I send the request]
}
Related
I am using the http client file to do requests in intellij (scratch.http), I want to parse the response and set a global variable. I saw here (https://www.jetbrains.com/help/idea/http-response-handling-examples.html#script-var-example) that I could use this:
POST https://httpbin.org/post
Content-Type: application/json
{
"token": "my-secret-token"
}
//Saving a variable
> {%
client.global.set("auth_token", response.body.json.token);
%}
But there is an issue, the endpoint I need to call returns the header Content-Type:application/json, but it's not really a json, because the response body starts with some characters, and then there is the json, so as an example:
)
]
}',
{
//normal json
}
how can I first slice the first characters of the response, and then parse the json to get the field that I want to set it as a global variable.
In postman I have this:
var jsonData = JSON.parse(responseBody.slice(5));
postman.setEnvironmentVariable("myField", jsonData.myField);
But I don't know how to do it with intellij http client.
I am trying to run a PACT test on the provider side and I don't know how to manipulate the request body that I get from the Pact file. I need to do this because I have to use an id from State step.
In my case, I need to perform a request in State step and afterwards to use the response of that request in the actual Pact verification test. So, I would like to replace a value from the pact file with the one obtained in the State.
Also, for being even more complicated, my body is an XML. So here it is how my pact request looks like:
"request": {
"method": "POST",
"path": "/path/url",
"headers": {
"Content-Type": "application/xml"
},
"body": "<note> <to>John</to> <from>Jane</from> <subject>Reminder</subject> </note>"
}
As I said, in the Provider State I will have a request and the response of this will be let's say 'Mary'. So my question would be how can I replace 'Jane' with 'Mary' in the Pact request body when executing the verification test? Thanks.
I have managed to solve my problem, modifying the request in TargetRequestFilter.
#TargetRequestFilter
public void updateRequest(HttpPost request) {
HttpEntity entity = request.getEntity();
String body = EntityUtils.toString(entity);
body = replace(body, "Jane", "Mary");
entity = new StringEntity(body);
request.setEntity(entity);
}
This piece of code will modify the request right before making the call and will send the desired value instead the one that we have in the Pact file.
in below three test cases Get Requests1, Get Requests2 and Get Requests3. I tried to get /RestResponse/result node value by three different way but it show same error
*** Settings ***
Library Collections
Library OperatingSystem
Library HttpLibrary.HTTP
Library RequestsLibrary
*** Test Cases ***
Get Requests1
# create a HTTP session to a server
Create Session countryname http://services.groupkt.com
# store response
${resp}= Get Request countryname /country/get/all
log ${resp.content}
# ${resp.content} display entire json response, now tring to get node value of /RestResponse/result
${getResponseJson} Get Json Value ${resp} /RestResponse/result
# shows error TypeError: expected string or buffer
log ${getResponseJson.content}
Get Requests2
# create a HTTP session to a server
Create Session countryname http://services.groupkt.com
# store response
${resp}= Get Request countryname /country/get/all
log ${resp.content}
# ${resp.content} display entire json response, now tring to get node value of /RestResponse/result
${responseContent}= to json ${resp.content}
# shows error TypeError: expected string or buffer
${getResponseJson} Get Json Value ${responseContent} /RestResponse/result
log ${getResponseJson.content}
Get Requests3
# create a HTTP session to a server
Create Session countryname http://services.groupkt.com
# store response
${resp}= Get Request countryname /country/get/all
log ${resp.content}
# ${resp.content} display entire json response,parse json
${data} Parse Json ${resp}
# shows error TypeError: expected string or buffer
${getResponseJson} Get Json Value ${resp} /RestResponse/result
Json file response
{
"RestResponse": {
"messages": [
"Total [249] records found."
],
"result": [
{
"name": "Afghanistan",
"alpha2_code": "AF",
"alpha3_code": "AFG"
},
{
"name": "\ufffd\ufffdland Islands",
"alpha2_code": "AX",
"alpha3_code": "ALA"
},
{
"name": "Albania",
"alpha2_code": "AL",
"alpha3_code": "ALB"
},
{
"name": "Algeria",
"alpha2_code": "DZ",
"alpha3_code": "DZA"
}
]
}
}
You need to call Get Json Value from the JSON, not from the request object. In your code, ${resp} is an object that includes the JSON data plus other things that json parsers don't know about.
${getResponseJson} Get Json Value ${resp.content} /RestResponse/result
${resp} is a python object. It has the json data, but it also has the HTTP return code and other information. You can't pass it to anything that accepts JSON.
${resp.content} is the body of the HTTP response. Like you write in your comments, this is the JSON data. Any keyword that accepts JSON data should accept this.
${resp.json} is the JSON string of the response converted to a python object. It is no longer JSON, it's a python dictionary. You cannot pass this to any function that requires JSON. You can, however, treat it like a normal python dictionary.
After running the code, ${getResponseJson} will have the data you expect. It is a unicode string, and unicode strings don't have a content attribute.
//body its like this
{
"to":
"/topics/NEWS"
,
"data":{
"extra_information": "This is some extra information"
},
//notification that i need to give
"notification":{
"title": "ChitChat Group",
"text": "You may have new messages",
"click_action":"ChatActivity"
}
}
The 401 error pertains that your Authorization Key is invalid or incorrect.
When using Postman, add a key= prefix for the value of Authorization, like so:
key=AAA...
See below for a tutorial on Sending Downstream FCM Messages using Postman.
Also, for your notification message payload, text isn't one of the valid parameters, I think you were looking for message instead.
Sending Downstream Messages using Postman
To do this in Postman, you simply have to set the following:
Set request type to POST
In the Headers, set the following:
Content-Type = application/json
Authorization = < Your FCM Server Key > (See your Firebase Console's Cloud Messaging Tab)
Set the payload parameters in the Body (*in this example, we used the raw option, see screenshot (2)*)
Send the request to https://fcm.googleapis.com/fcm/send
Screenshots:
(1)
Note: Always keep your Server Key a secret. Only a portion of my key is visible here so it should be fine.
(2)
(3)
Notice that the request was a success with the message_id in the response.
Wrong:
Authorization:AIzaSyDDk77PRpvfhh......
Correct:
Authorization:key=AIzaSyDDk77PRpvfhh......
Full example:
https://fcm.googleapis.com/fcm/send
Content-Type:application/json
Authorization:key=AIzaSyZ-1u...0GBYzPu7Udno5aA
{ "data": {
"score": "5x1",
"time": "15:10"
},
"to" : "bk3RNwTe3H0:CI2k_HHwgIpoDKCIZvvDMExUdFQ3P1..."
}
While the answers above are still correct, you may choose to use HTTP v1. This requires Bearer instead of key= and uses an Oauth2 access token instead of a server key string. To view HTTP v1 specifications, please refer to the link below:
https://firebase.google.com/docs/cloud-messaging/migrate-v1
I was also getting same error in PHP , solved with below header :
$header = array("authorization: key=" . $this->apiKey . "","content-type: application/json");
The code i am trying with is:-
response = HTTPotion.post(url, [body: "{channel: \"#bot\", username: \"watson\", text: \"test\"}"])
The response i am getting is:-
%HTTPotion.Response{body: "invalid_payload",......, status_code: 400}
You made a successful request, but the body was wrong. In JSON there should be quotes around the field names:
[body: "{\"channel\": \"#{bot}\", \"username\": \"watson\", \"text\": \"test\"}"]
Also the syntax for string interpolation is #{variable_name} for example:
iex(1)> bot = "mybot"
iex(2)> "#{bot}"
Manually encoding JSON is error prone so you probably want to use Poison.
iex(3)> Poison.encode!(%{bot: bot, username: "watson", text: "test"})
"{\"username\":\"watson\",\"text\":\"test\",\"bot\":\"mybot\"}"