Sending \n in POST request

newfish

New member
I'm having trouble sending properly formatted json string containing multiple lines inside a var, in a HTTP POST. The responses from the server have no trouble encoding newlines as '\n', and that shows up in resulting var...no matter what way I try to sneak them in (multiple escapes, etc) the server returns an 'improperly formatted' error.

What gives? How can I encode newlines inside a json string? Any help greatly appreciated.

To be clear, an example would be:



JSON:
{
  "id": "cmpl-uqkvlQyYK7bGYrRHQ0eXlWi7",
  "object": "text_completion",
  "created": 1589478378,
  "model": "text-davinci-002",
  "choices": [
    {
      "text": "\n\nThis is a test",
      "index": 0,
      "logprobs": null,
      "finish_reason": "length"
    }
  ],
  "usage": {
    "prompt_tokens": 5,
    "completion_tokens": 6,
    "total_tokens": 11
  }
}

(note the newlines in "text")
 

newfish

New member
Fwiw, I had to use termux:tasker and use bash to get the escape sequences into the json string.
With var 'prompt' having actual newlines inside macrodroid, one can use this bash substitution to convert them to the escapes:

Code:
varz='%prompt'
v="${varz//$'\n'/\\n}"

...and then use $v in your script
 

newfish

New member
...what would be great: a checkbox in the var settings that enables encoding newlines as \n, so reliance on plugins to do http requests wasn't necessary...but I'm not complaining, macrodroid is the coolest thing that's ever happened to mobile. Thank you to the devs!
 

Qarboz

Well-known member
...what would be great: a checkbox in the var settings that enables encoding newlines as \n, so reliance on plugins to do http requests wasn't necessary...but I'm not complaining, macrodroid is the coolest thing that's ever happened to mobile. Thank you to the devs!
You can submit your request here, it looks interesting
 
Top