vscode-restclient How to process / parse response body - visual-studio-code

The vscode extension vscode-restclient allows to create http request and handle the response similar to curl or postman.
A POST request to /sales/getResult/ returns this response
HTTP/1.1 200 OK
Date: ....
Content-Type: text/plain; charset=utf-8
Content-Length: 67
Connection: close
Load your results with ID: CJoYTvh8
From the body we need the id CJoYTvh8 to get details. The request to get details goes to the URL /sales/GetResult/{{resultId}} . Where {{resultId}} stands for the ID CJoYTvh8 from the previous response body.
I want to create the request for the details which needs to look like this
#resultId = {{myRequest.response.body}} // this should only be CJoYTvh8
# #name getResult_for_id
POST /sales/getResult/{{resultId}} HTTP/1.1
Host: {{our_host}}
Authorization: Bearer {{authToken}}
Content-Type: application/json
I am looking for something like this
#resultId = response.body.split(':')[1].trim()
Question
How can i split the string Load your results with ID: CJoYTvh8 in vscode-restclient so that i assign only the id CJoYTvh8 to the variable #resultId?

Related

Unable to validate response headers from a REST API using Karate

I have a simple REST API that allows to POST resources in a given endpoint, and I want to validate that the response headers are set correctly. I have defined the following feature, which inserts a bunch of users in the database:
Scenario Outline: Creating a bunch of new users when the database is empty
Given header Content-Type = 'application/json'
And request __row
When method post
Then status 201
* match header Content-Type == 'application/json'
* match header Location == 'http://localhost:8080/users/'+response.id
Examples:
| users |
Users are inserted correctly, and in console I can see the following response headers:
1 < 201
1 < Connection: keep-alive
1 < Content-Type: application/json
1 < Date: Mon, 22 Feb 2021 14:06:31 GMT
1 < Keep-Alive: timeout=60
1 < Location: http://localhost:8080/users/user1#test.com
1 < Transfer-Encoding: chunked
But each time I try to run this test, I get the same response:
path: $['Content-Type'][0], actual: null, expected: 'application/json', reason: actual json-path does not exist
Works for me on 0.9.6
* url 'https://httpbin.org/get'
* method get
* match header Content-Type == 'application/json'
But as I mentioned in my comment, please try the 1.0 / RC version: https://github.com/intuit/karate/wiki/1.0-upgrade-guide
As we are focusing efforts on that.

Bad Request While Trying to create transient document in adobe using REST API

I am trying to create a transient document in adobe sign using rest api.Foloowing is my code snippet:
Http h = new Http();
HttpRequest req = new HttpRequest();
req.setEndpoint('https://api.in1.echosign.com/api/rest/v6/transientDocuments');
req.setMethod(postMethod);
req.setHeader('Content-Type', 'application/json');
blob testFileContent = blob.toPDF('test string it is');
// req.setBody('{"filename":"testsign","file":'+testFileContent+'}');
req.setBody('{"fileName":"testsign","file":"'+testFileContent+'"}');
String authorizationHeader = 'Bearer ' +acceessToken;
req.setHeader('Authorization', authorizationHeader);
//req.setHeader('Authorization', acceessToken);
try{
HttpResponse res = h.send(req);
However I am getting the bad request error in response.Issue maybe due to the tag mismatch with adobe sign requirement, however after a lot of research and trial and error I am not able to find the accurate tags to put in JSON for this API request. Any suggestion and help ?
Form data
Content-Disposition: form-data; name="File"; filename="<filename>.pdf"
Content-Type: application/pdf
Request params
Content-Type: multipart/form-data;
Accept: application/json, text/javascript, */*;
More details available in the documentation.

Load performance testing with Gatling and Content-Type

I am using gatling for load performance testing on a brand new API. It seems fairly easy and well documented but I am facing an issue as simple as POST a request with Content-Type set to 'application/vnd.api+json' on the Header. All works well when doing the GET stuff but when launching a POST test I get a
HTTP response:
status=
415 Unsupported Media Type
headers=
cache-control: [no-cache]
Content-Type: [application/vnd.api+json; charset=utf-8]
Date: [Fri, 08 Sep 2017 12:57:10 GMT]
Server: [nginx]
Vary: [Origin]
x-content-type-options: [nosniff]
x-frame-options: [SAMEORIGIN]
X-Request-Id: [ff993645-8e01-4689-82a8-2f0920e4f2a9]
x-runtime: [0.040662]
x-xss-protection: [1; mode=block]
Content-Length: [218]
Connection: [keep-alive]
body=
{"errors":[{"title":"Unsupported media type","detail":"All requests that create or update must use the 'application/vnd.api+json' Content-Type. This request specified 'application/json'.","code":"415","status":"415"}]}
Here is the scala code I am using for the http request:
object PostTokenGcm {
val token = exec {
http("TestAPI POST /tokens")
.post("/tokens")
.headers(Map("Authorization" -> testApiToken,
"Content-Type" -> "application/vnd.api+json",
"Accept" -> "application/vnd.api+json" ))
.body(StringBody(gcmTokenRequestBody)).asJSON
.check(status.is(201))
.check(bodyString.exists)
}}
It seems that it is not setting the Content-Type?
Thank you for any lead!
In your POST definition you're using asJSON. According to notes in documentation about request headers:
http("foo").get("bar").asJSON is equivalent to:
http("foo").get("bar")
.header(HttpHeaderNames.ContentType, HttpHeaderValues.ApplicationJson)
.header(HttpHeaderNames.Accept, HttpHeaderValues.ApplicationJson)
... so, headers set in:
.headers(Map("Authorization" -> testApiToken,
"Content-Type" -> "application/vnd.api+json",
"Accept" -> "application/vnd.api+json" ))
... get overwritten by asJSON to "application/json" (which is the value of HttpHeaderValues.ApplicationJson).

Titanium multipart file upload with different content types?

So I am trying to post to a Salesforce.com REST api that is expecting the following sample request -
POST /services/data/v33.0/chatter/feed-elements HTTP/1.1
Authorization: OAuth 00DRR0000000N0g!...
User-Agent: Jakarta Commons-HttpClient/3.0.1
Host: instance_name
Content-Length: 845
Content-Type: multipart/form-data; boundary=a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Accept: application/json
--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Content-Disposition: form-data; name="json"
Content-Type: application/json; charset=UTF-8
{
"body":{
"messageSegments":[
{
"type":"Text",
"text":"Please accept this receipt."
}
]
},
"capabilities":{
"content":{
"description":"Receipt for expenses",
"title":"receipt.pdf"
}
},
"feedElementType":"FeedItem",
"subjectId":"005RR000000DmOb"
}
--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq
Content-Disposition: form-data; name="feedElementFileUpload"; filename="receipt.pdf"
Content-Type: application/octet-stream; charset=ISO-8859-1
...contents of receipt.pdf...
--a7V4kRcFA8E79pivMuV2tukQ85cmNKeoEgJgq--
As you can see, the two parts of the request is expecting different content types.
In Titanium, I have the following code -
xhr.open("POST", postUri);
xhr.setRequestHeader("Authorization", authHeader);
xhr.setRequestHeader('enctype', 'multipart/form-data');
var data2send = {
json: jsonObj,
feedElementFileUpload: imageBlob
};
xhr.send(data2send);
According to the Appcelerator documentation and a couple of online threads, the httpclient should be able to set each part automatically, however, Salesforce API still gives an error message of:
[{"errorCode":"MISSING_ARGUMENT","message":"Missing required 'subjectId' parameter."}]
I can confirm that subjectId is in the jsonObj and its content is correct. It seems that the json part is not recognised as JSON by Salesforce correctly. How can I set the content-type correctly? Many thanks.
Updates - I used the Wireshark to capture the request text. Apparently the httpclient did not set up the content-type for the json part that Salsforce API is expecting. Any idea how to set it?
POST /services/data/v33.0/chatter/feed-elements HTTP/1.1
Host: mydomain.com
Accept-Language: en-us
User-Agent: Appcelerator Titanium/3.5.1 (iPhone Simulator/8.2; iPhone OS; en_US;)
enctype: multipart/form-data
X-Requested-With: XMLHttpRequest
Accept: */*
Content-Type: multipart/form-data; charset=utf-8; boundary=0xTibOuNdArY_1430987526
Connection: keep-alive
Authorization: Bearer $My_AUTH_CODE
X-Titanium-Id: 8329dd1d-3379-4d7c-955e-120ad1586a2b
Content-Length: 93333
Accept-Encoding: gzip, deflate
--0xTibOuNdArY_1430987526
Content-Disposition: form-data; name="json"
{"feedElementType":"FeedItem","subjectId":"xxxxxxxxxx","body":{"messageSegments":[{"type":"text","text":"test"}]},"capabilities":{"content":{"description":"sdfaadfs","title":"adsfafsd.png"}}}
--0xTibOuNdArY_1430987526
Content-Disposition: form-data; name="feedElementFileUpload"; filename="01430987526.png"
Content-Type: image/png
my file data...

Passing parameters to WCF restful service with POST method

how can I use Fiddler to pass 2 parameters to a method with a signature like this :
[WebInvoke(UriTemplate = "Login", Method = "POST", ResponseFormat = WebMessageFormat.Xml, BodyStyle = WebMessageBodyStyle.WrappedRequest)]
bool Login(string login, string password);
Any suggestion to change something in the method is welcome, but I have always to pass 2 string parameters.
It is much easier to the services with WCFTestClient. Anyway, Fiddler has composer functionality which can be used to test any http calls.
Request Headers should look like below. Just change SOAPAction accordingly.
Content-Type: text/xml; charset=utf-8
SOAPAction: "http://tempuri.org/IService/Login"
Host: localhost:2045
Content-Length: 179
Expect: 100-continue
Accept-Encoding: gzip, deflate
Proxy-Connection: Keep-Alive
And request body:
<s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/"><s:Body><Login xmlns="http://tempuri.org/"><login>sdf</login><password>sdf</password></Login></s:Body></s:Envelope>