Problem with JSON body in POST request

  • Hello everyone,
    I’m having a problem.
    When making a POST request with a JSON body, I refer to the previous data to insert the link {{ $json.link }} into the body.

    The problem is that when I use {{ $json.link }} I get the error “JSON parameter needs to be valid JSON [item 0]”.

    But if I manually insert the link instead of {{ $json.link }},
    the request is executed successfully. What could be the problem?

  • Body structure:

  • [
        {
            "method": "POST",
            "path": "",
            "params": {
                "object": "",
                "id": 
            },
            "postData": {
                "path": "",
                "type": "",
                "data": "*page html code*"
            }
        }
    ]
    

Hey @Kovach hope all is good. Would you like to share your workflow?

Unfortunately, I cannot share the project, as it uses a private API key and the “states=” parameter, which requires updating for each subsequent request :frowning:

Can you share just the HTTP Request node? State, cookies or other nodes wouldn’t matter.

Inside the body request

“data”: "*page html code*”

*page html code* contains:
href="{{ $json.link }}"

Where
{{ $json.link }} = https://example.com/

In this way, I’m getting error in POST response:

JSON parameter needs to be valid JSON [item 0]

But if I manually insert the link
href="https://example.com"

Everything works fine

Hi Kovach
Can you share your screen?

Hi @Kovach

Did you try this:

href=\{{ JSON.stringify( $json.link ) }}\

I am almost sure It is not going to be about how you do it, but rather about the content, for instance here is an example of the relevant parts from your problem description and it works just fine:

This solution did not help :frowning:

In this case, where 
{{ $json.link }} = https://example.com, the solution returns the error “JSON parameter needs to be valid JSON [item 0]”.

[
    {
        "method": "POST",
        "path": "",
        "params": {
            "object": "",
            "id": 12345
        },
        "postData": {
            "path": "",
            "type": "",
            "data": "<!doctype html><title>Hello</title><p>Hello, world! <a href=\"{{ $json.link }}\">Visit Example</a></p>"
        }
    }
]

In case 

[
    {
        "method": "POST",
        "path": "",
        "params": {
            "object": "",
            "id": 12345
        },
        "postData": {
            "path": "",
            "type": "",
            "data": "<!doctype html><title>Hello</title><p>Hello, world! <a href=\"https://example.com\">Visit Example</a></p>"
        }
    }
]

Everything works perfectly


What do you mean “the solution returns the error”?

Here is an idea, can you provide an example where it fails, so that we are working on your non-working example instead

1 Like

Thank you all for your help! The problem was probably caused by the HTML code in “data” being too long. In the end, I managed to complete the task by converting the HTML code into a base64 string.

1 Like

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.