OS Version: Windows 10 Home, Build Ver 1709
REST Client Version: 0.18.1
Steps to Reproduce:
###
# @name session_token
GET {{baseUrl}}/services/session/token HTTP/1.1
content-type: application/json
###
# @name login
POST {{baseUrl}}/api/user/login HTTP/1.1
content-type: application/json
X-CSRF-Token: {{login_token}}
test: {{session_token.response.body}}
{
"some" : "json"
}
Sending session_token returns body:
Send request login actually sends (as caught by fiddler):
POST /api/user/login HTTP/1.1
content-type: application/json
X-CSRF-Token: 9m5UzOssDiuKpqlYb0HPIlPy7i7Q4ulBDCde34t80hc
test: {{session_token.response.body}}
User-Agent: vscode-restclient
host: some_host
accept-encoding: gzip, deflate
content-length: 76
Connection: keep-alive
I get a warning for a line "test: {{session_token.response.body}}"
"owner": "_generated_diagnostic_collection_name_#0",
"code": "undefined",
"severity": 4,
"message": "Body path should be provided right after \"body\"",
For now I have to copy the token manually to a variable but according to the docs I should be able to use like test: {{session_token.response.body}} unfortunetly session_token.response.body does not gets resolved
In this case, what seems to be expected is that the whole body is saved in variable
@abenedykt under the premise that content type of response body should be json or xml, we could successfully extract the body value. And if your response body actually matches the case, you should provide the JSONPath or XPath to extract the expected part as described in warning message. In your case, if your response body is only filled with 9m5UzOssDiuKpqlYb0HPIlPy7i7Q4ulBDCdeaaaaaaaaaa, it should work like {{session_token.response.body.$}}
@Huachao ok thats the thing I've didn't noticed. This string is not a valid json :( lame of me.
{{session_token.response.body.$}}
doesn't get resolved. Oh well, I guess I'll have to copy tokents manually.
The response is only withhed with 9m5UzOssDiuKpqlYb0HPIlPy7i7Q4ulBDCdeaaaaaaaaaa (but it's not a valid json). Maybe it could be a feature request to actually copy the whole response.
@abenedykt yes, and what's your Content-Type of your response?
Content-Type: text/plain;charset=UTF-8 :(
Setting accept doesn't help.
@abenedykt got, as you said previously, I should implement a selector to fetch the whole response body, instead of extracting specific attribute of json and xml responses.
that would be awesome :)
I was just about to open an issue on selecting the whole body in an XML request.
# @name getxml
GET https://httpbin.org/xml
###
# selects top level children - but not the root node
POST https://httpbin.org/post
{{getxml.response.body.//*}}
###
# doesn't work
POST https://httpbin.org/post
{{getxml.response.body./}}
I am a bit rusty in XPath, but I would expect //* to select the top level children - which it does.
I was trying to select the root element, where / would be a valid xpath for that, but that is not accepted in the plugin.
If {{getxml.response.body}} just returned the whole body, that would solve my problem.
@lldata I have pushed a new commit to support {{getxml.response.body./}} which I think it's a bit of different from the semantic. The {{getxml.response.body./}} means getting since the root element(comment node is not included), while the requirement here for getting the whole response content, doesn't take the XML DOM into consideration, just simply pipe the response body to another request. And I'd like to support both, and the former case fits your case. Supporting piping the whole body would be done later.
@lldata you can resolve the xml response body by using {{getxml.response.body./}} in latest version 0.18.2, but as I stated before, it's not the same as the requirement that fetch the whole response body without considering the response content type.
I just started to use this extension in complement with Postman. Very useful extension.
Instead of {{session_token.response.body.$}}, can {{session_token.response.body.*}} be used to get all the body content regardless Content-Type. I think that only need a minor change in requestVariableCacheValueProcessor.ts.
@sanpee @abenedykt @lldata @mladengit I have adopted @sanpee's suggestion, and use the syntax {{request_variable_name.response|request.body.*}} to retrieve the whole response body regardless of the content-type.
@sanpee @abenedykt @lldata @mladengit @frankwb24 you can try the latest version 0.20.0 to verify this fix.
It works perfectly. Thank you.
I opened a new issue for a specific use https://github.com/Huachao/vscode-restclient/issues/266
HTTP/1.1 200 OK
Date: Mon, 25 Feb 2019 10:32:58 GMT
Content-Length: 70
Expires: Mon, 25 Feb 2019 10:32:58 GMT
Cache-Control: no-cache
Content-Type: application/json-rpc
Access-Control-Allow-Origin: *
Content-Encoding: gzip
Connection: close
{
"id": "1",
"jsonrpc": "2.0",
"result": "bd8fcd73a15c681f"
}
This is the response of aria2c, but i can't use "@hashValue = {{addUri.response.body.$.result}}"
how can i solve this problem?
@xywwf since the Content-Type of response is not application/json, I will consider if I can improve this, thanks for mentioning this
@xywwf since the
Content-Typeof response is notapplication/json, I will consider if I can improve this, thanks for mentioning this
It seems that the question not been solved in the new version, i read the file vscode-restclient/src/utils/requestVariableCacheValueProcessor.ts, and found the code just like below
const contentTypeHeader = getHeader(headers, 'content-type');
if (MimeUtility.isJSON(contentTypeHeader)) {
const parsedBody = JSON.parse(body as string);
return RequestVariableCacheValueProcessor.resolveJsonHttpBody(parsedBody, nameOrPath);
} else if (MimeUtility.isXml(contentTypeHeader)) {
return RequestVariableCacheValueProcessor.resolveXmlHttpBody(body, nameOrPath);
} else {
return { state: ResolveState.Warning, value: body, message: ResolveWarningMessage.UnsupportedBodyContentType };
}
I wonder if it's possible to distinguish the response body by parameter nameOrPath that user given ?
@xywwf personally I think we only need to update the JSON MIME type validation logic and make your response pass the check
Most helpful comment
@abenedykt got, as you said previously, I should implement a selector to fetch the whole response body, instead of extracting specific attribute of
jsonandxmlresponses.