I am using postman to test api. The method is POST and JSON data is:
{ "page" : "<xml> <content xs="xyz">.......</xml>" }
In body I have selected 'raw' and selected 'JSON' which automatically adds 'application/json' to the header.
When I send this request, I get error due to the double quotes in the <content> tag. Does this need handling (escaping) at input or inside the api code?
You have to escape double quotes with \, e.g.
{"page":"<?xml version=\"1.0\"?>\n<page></page>"}
Also make sure you set HTTP header Content-Type: application/json.
Related
Setup
Python FastApi backend server
JS React frontend client
react-use-websocket for frontend ws client
Issue
So I need to send some messages from backend API to frontend client over Websocket connections and display these messages to user. However I don't know how to escape to a new line while displaying these messages. I tried sending \n inside the Websocket message. JS doesn't display this \n character but neither does switch to a newline. It just ignores it. Also tried sending some whitespace but that didn't help either. It just shows a single space regardless of how many whitespaces I left at the API side.
Code
note: TextTransition component is from react-text-transition but the
behavior is same with a standart html paragraph.
const {lastJsonMessage} = useWebSocket(socketURL);
//...
return (
<div>
...
...
<div style={boxStyle}>
<h1 style={{cursor: "default"}}>Message</h1>
<p style={{cursor: "default"}}>━━━━━━━━━━━━━━━━━</p>
<TextTransition
text={lastJsonMessage ? lastJsonMessage.message : ""}
style={{cursor: "default"}}
springConfig={ presets.stiff }
/>
</div>
...
...
</div>
)
What can and should I do? Thanks in advance.
\n does not cause a new line in HTML. You will either need to send <br/> instead, or replace \n on the client side with that, for instance:
<TextTransition
text={lastJsonMessage ? lastJsonMessage.message.replace(/\n/g, '<br/>') : ""}
style={{cursor: "default"}}
springConfig={ presets.stiff }
/>
This assumes that TextTransition accepts HTML (not just text). If the latter then you may be out of luck, because that component just won't let you set control characters in your text.
JSON utilises double-quotes.
My environmental constraint is that I must place HTML attributes in double-quotes.
I know I can encode JSON using:
URL Encoding (via encodeURI(JSONToEncode))
Base64 encoding (via window.btoa(JSONToEncode))
But how can I straightforwardly include JSON in an HTML5 data-* attribute so that it remains (maximally) human-readable as well as machine-readable?
eg. How can I include this:
{"chosenStarter" : "bhajis", "chosenMain" : "madras", "chosenDessert" : "kulfi"}
in this:
data-menu="[... VALUE HERE...]"
One straightforward human-readable approach is to use angular quotes:
data-menu="{«chosenStarter» : «bhajis», «chosenMain» : «madras», «chosenDessert» : «kulfi»}"
and then convert to JSON with:
const myJSON = myElement.dataset.menu.replace(/«|»/g, '"');
I'm working with Postman right now and I have to do a lot of requests, and in the body I'm passing a JSON content. I would like to know if is there a way to pass the value of a global variable into JSON body. Thank you.
If using a raw JSON body:
{
"test key":"{{global variable}}"
}
On sending the request, the curly braces will be replaced with the value of the variable.
I think what you want to do is described here.
To use a variable you need to enclose the variable name with double curly braces – {{my_variable_name}}.
Double curly bracket is working in header parameter, url or inside JSON body. Inside tests you should use globals example: {"url": globals.url} or {"url": globals["url"]}
You can pass
{
"productId": {{**ProductID**}},
"quantity": 1
}
Where ProductID is your global variable name
in raw format JSON (application/json)
Yep, double curly brackets is the way to go to achieve this - make sure you have the latest version of Postman (though if you are still running 2014 code when this feature was introduced, shame on you!)
eg:
{
"variable": "{{value}}"
}
See the second paragraph here in the Variables section of the documentation - it specifically mentions the request body.
I want to put the value="name+asc" in hidden field. Http method is Get.
<input type="hidden" value="name+asc" name="sort" />
But I cannot put the name+asc in URL.
How can I handle that problem.
Values of form inputs will be automatically encoded correctly when a form is submitted. In application/x-www-form-urlencoded data, a + sign represents a space so the browser will encode it as %2B.
If you want to submit a form with + in the data: The code you have is fine.
If you want to submit a form with a space in the data: Use a literal space. The browser will encode it for you.
If you want to read form data with JavaScript and construct a URL (or post request) programatically when pass the data through the encodeURIComponent method.
var value = "123+456";
var encodedValue = encodeURIComponent(value);
var url = "http://example.com/?query=" + encodedValue;
If you want to construct a URI by hand (e.g. for pasting into an HREF attribute) then you will need to look up the encoded values … somewhere. I tend to run node.js in a terminal so I can quickly type encodeURIComponent('a string literal');.
As far as i know the + will automatically split your values unless encoded to special characters. You can use a - or a _ instead of the +.
According to RFC 4180:
...the presence or absence of the header line
should be indicated via the optional "header" parameter of this
MIME type...
So, does that mean the correct string is:
text/csv; header
Or perhaps:
text/csv; header=true
Or something else?
The "header" parameter indicates the presence or absence of the header line. Valid values are "present" or "absent".
So if you use that parameter, the full MIME type would be text/csv; header=present or text/csv; header=absent.