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

parseRequest can't deal with a JSON massive of objects #481

Open
ddd356 opened this issue Jan 31, 2022 · 3 comments
Open

parseRequest can't deal with a JSON massive of objects #481

ddd356 opened this issue Jan 31, 2022 · 3 comments

Comments

@ddd356
Copy link

ddd356 commented Jan 31, 2022

{-# LANGUAGE OverloadedStrings #-}
module Main where

import Lib
import Network.HTTP.Simple

req1 = parseRequest_ "https://host.com/method/messages.send?keyboard=\"\""
req2 = parseRequest_ "https://host.com/method/messages.send?keyboard={\"buttons\":true}"

req3 = parseRequest_ "https://host.com/method/messages.send?keyboard={\"buttons\":[{\"prop\":\"val\"}]}"

main :: IO ()
main = putStrLn "main"

Hello! I try to parse URL where one of parameters is a JSON with a nested list of objects.
parseRequest throws exception on req3 in this code.
In req3 URL is incorrect, because of symbols, that can't be part of URL, but maybe parser can correctly encode by himself.
Think it's a bug. Check it please.

@snoyberg
Copy link
Owner

This doesn't appear to be a bug, it looks like you have incorrectly escaped query string parameters. I'd recommend using a library for properly building up URLs that handles escaping correctly.

@ddd356
Copy link
Author

ddd356 commented Jan 31, 2022

But one thing still embarrassing me.
That's when I use setRequestQueryString to set same JSON string to one of the parameters, it's works with no exceptions. It makes me think, that parser must do the same thing with a string.
Function req4 works right.

req4 = setRequestQueryString [ ("keyboard", Just "{\"buttons\":[{\"prop\":\"val\"}]] . setRequestPath "/method/messages.send" . setRequestPort 443 . setRequestSecure True . setRequestHost "myhost.com" $ defaultRequest

@robx
Copy link
Collaborator

robx commented Feb 8, 2022

@ddd356 That's because you're not parsing the URL in your last example. setRequestQueryString encodes the query parameters properly:

> getUri $ setQueryString [("foo", Just "[]")] defaultRequest
http://localhost/?foo=%5B%5D

The resulting URI can be parsed just fine:

> getUri $ parseRequest_ "http://localhost/?foo=%7B%7D"
http://localhost/?foo=%7B%7D

While unescaped brackets can't be parsed:

> getUri $ parseRequest_ "http://localhost/?foo=[]"
*** Exception: InvalidUrlException "http://localhost/?foo=[]" "Invalid URL"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants