Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Post request and conversion to binary type #751

Closed
errshoff opened this issue Jul 25, 2022 · 7 comments
Closed

Post request and conversion to binary type #751

errshoff opened this issue Jul 25, 2022 · 7 comments
Labels
type/bug Something isn't working

Comments

@errshoff
Copy link

Hi all!
I need to send a POST request from my script. This is example code:

LET req = {hello: "world"}
LET result = IO::NET::HTTP::POST({
	url: "https://httpbin.org/post",
	body: JSON_STRINGIFY(req),
	headers: {"Content-Type": "application/json"}
})

RETURN JSON_PARSE(TO_STRING(result))

But when:
$ ferret exec post.fql

I get the following error:
invalid type: expected [binary], but got string: .body: IO::NET::HTTP::POST({url:"https://httpbin.org/post",body:JSON_STRINGIFY(req),headers:{"Content-Type":"application/json"}}) at 3:13

And that's right...
But how do I convert string to binary?
I did not find such a function in the documentation

@Gusyatnikova
Copy link

Hello!

You can use to_binary function:
Let binaryBody = to_binary('"id"=15')

Hope it helps,
Natalia

@errshoff
Copy link
Author

Does not work:
compile query: not found: function: 'TO_BINARY'

@ziflex ziflex added the type/bug Something isn't working label Jul 30, 2022
@3timeslazy
Copy link
Member

3timeslazy commented Aug 10, 2022

@errshoff didn't you try to pass req instead of JSON_STRINGIFY(req)?

@errshoff
Copy link
Author

@3timeslazy I tried. The result is the same:
invalid type: expected [binary], but got object

@leopku
Copy link

leopku commented Oct 11, 2022

Same issue. not found: function: 'TO_BINARY'
@errshoff , should you plz sharing how did you fixed it?

@Gusyatnikova
Copy link

Gusyatnikova commented Oct 11, 2022

Hi, @leopku, you can try to add by yourself something like this:

  1. To pkg/stdlib/types file to_binary.go with function
func ToBinary(ctx context.Context, args ...core.Value) (core.Value, error) {
	err := core.ValidateArgs(args, 1, 1)

	if err != nil {
		return values.None, err
	}

	val := args[0].String()

	return values.NewBinary([]byte(val)), nil
}
  1. To pkg/stdlib/types/tib.go add raw to RegisterLib func
    "TO_BINARY": ToBinary,

  2. In your script use
    `Let binaryBody = to_binary('"id"=15')
    LET result = IO::NET::HTTP::POST({

    url: "https://httpbin.org/post",

    body: binaryBody,

    headers: {"Content-Type": "application/json"}

})
`

The reason of this error is absence of to_binary in ferret

@leopku
Copy link

leopku commented Oct 11, 2022

@Gusyatnikova thanks a lot.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/bug Something isn't working
Projects
None yet
Development

No branches or pull requests

5 participants