HTTP request (square brackets in JSON conflicts with Switch variables)

Post Reply
User avatar
magnussandstrom
Advanced member
Posts: 365
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

HTTP request (square brackets in JSON conflicts with Switch variables)

Post by magnussandstrom »

Hi, I'm bulding a flow where I want use the openAI API.

I'm using the latest version of HTTP request app (v19) and want to do the following:

Request type: POST a body
Body content: Custom > Multi-line text with variables

Where I would like to populate with the following JSON:

Code: Select all

'{
  "model": "gpt-4-1106-preview",
  "messages": [
    {
      "role": "user",
      "content": "Write a poem about flowers"
    }
  ],
  "temperature": 1,
  "max_tokens": 256,
  "top_p": 1,
  "frequency_penalty": 0,
  "presence_penalty": 0
}'
...but since the JSON already contains [square brackets] I cannot use Switch variables to populate the JSON with my own data.

The only work-a-round I could figure out is to:
1. Create the JSON as a textfile (Create text file-app) but replace the square brackets with %startbracket% %endbracket%
2. Use String replace-app to convert %startbracket% %endbracket% to square brackets again
3. Post the textfile with HTTP request: POST > Attached file > [Job.Path]

But I guess that there is a better way, any suggestions?
Padawan
Advanced member
Posts: 358
Joined: Mon Jun 12, 2017 8:48 pm
Location: Belgium
Contact:

Re: HTTP request (square brackets in JSON conflicts with Switch variables)

Post by Padawan »

You can escape square brackets in switch variables by duplicating the opening brackets.

Can you try this?

Code: Select all

'{
  "model": "gpt-4-1106-preview",
  "messages": [[
    {
      "role": "user",
      "content": "Write a poem about flowers"
    }
  ],
  "temperature": 1,
  "max_tokens": 256,
  "top_p": 1,
  "frequency_penalty": 0,
  "presence_penalty": 0
}'
User avatar
magnussandstrom
Advanced member
Posts: 365
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: HTTP request (square brackets in JSON conflicts with Switch variables)

Post by magnussandstrom »

Thanks Padawan! I did not know about that escaping method, the escaping works but I stumbled on another issue.

I get a 400 response (Bad request) when using the latest HTTP request v19 app. I tried both 'POST a body' and POST with file > [Job.Path]

When I roll back to the older HTTP request v15 and use POST with file > [Job.Path], it works fine and I get the expected response.

I have trippled checked that I'm using the same settings, but some how I can't get the HTTP request v19 to work..

Any suggestions?
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: HTTP request (square brackets in JSON conflicts with Switch variables)

Post by freddyp »

Are you using custom headers?
User avatar
magnussandstrom
Advanced member
Posts: 365
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: HTTP request (square brackets in JSON conflicts with Switch variables)

Post by magnussandstrom »

Yes, the Headers property in the app are set to Multi-line text with variables and are populated with:

Content-Type: application/json
Authorization: Bearer ********


I can see that they seem to be handled correctly in Messages/Debug:

log.png
log.png (127.16 KiB) Viewed 20814 times
freddyp
Advanced member
Posts: 1023
Joined: Thu Feb 09, 2012 3:53 pm

Re: HTTP request (square brackets in JSON conflicts with Switch variables)

Post by freddyp »

There was a problem with application/octet-stream always being added in case of a POST, thus overwriting the custom header value. There will be a beta version of HTTP request v20 next Tuesday. If you are on the list of beta users you will receive a notification of that. If not, send me a mail and I will add you. I will be looking forward to your feedback.
User avatar
magnussandstrom
Advanced member
Posts: 365
Joined: Thu Jul 30, 2020 6:34 pm
Location: Sweden
Contact:

Re: HTTP request (square brackets in JSON conflicts with Switch variables)

Post by magnussandstrom »

Thanks Freddy, I’ll send you an email.
Post Reply