Help with formating body/REST

Hello

first me code:)

	urlStr := "http://172.31.1.193/rt/REST/1.0/ticket/new?user=root&pass=asdasd"
	/*	resource := "/user/"


		u, _ := url.ParseRequestURI(apiUrl)
		u.Path = resource
	urlStr := u.String() // "https://api.com/user/"*/

	b := `content=id: ticket/new
	Queue: General
	Requestor: asd@asd.saaai
	Priority: 1
	Subject: asdasdd test 4
	Text: 12321321321321321321321321321321321321`

	fmt.Println(bytes.NewBufferString(b))
	fmt.Println(strconv.Itoa(len(b)))

	client := &http.Client{}
	r, _ := http.NewRequest("POST", urlStr, ioutil.NopCloser(bytes.NewBufferString(b))) // URL-encoded payload
	r.Header.Add("Content-Type", "application/x-www-form-urlencoded")
	r.Header.Add("Content-Length", strconv.Itoa(len(b)))

	resp, _ := client.Do(r)
	fmt.Println(resp)

Problem is that the server on other side does not read this well. From what I noticed is that (tracing with wireshark) golang formats this string for every newline with \t\n, but when I send this request via curl, there is only \n.

With curl is like:
Key: content
Value: id: ticket/new\nQueue: General\nRequestor:someting@something…

With go is linke:
Key:content
Value: id: ticket/new\t\nQueue: General\t\nRequest:something@something…

how can i format this that go will send without \t.

thank you.

Well you indented the lines in the string, so whatever you used to indent will be sent literally to the other side. Remove the indentation in the string and it should work then.

As @NobbZ suggests, you’re actually indenting the response in your code and Go is simply respecting that.

The \t literally comes from here in your code.

19

You could just remove the indentation from your code like this:

b := `content=id: ticket/new
Queue: General
Requestor: asd@asd.saaai
Priority: 1
Subject: asdasdd test 4
Text: 12321321321321321321321321321321321321`

Or alternatively if the line breaks aren’t critical, you could just send it as an inline string.

b := `content=id: ticket/new Queue: General Requestor: asd@asd.saaai Priority: 1 Subject: asdasdd test 4 Text: 12321321321321321321321321321321321321`

If the line breaks are critical, you could do the above and manually include the line breaks with \n. Like this:

b := `content=id: ticket/new\nQueue: General\nRequestor: asd@asd.saaai\nPriority: 1\nSubject: asdasdd test 4\nText: 12321321321321321321321321321321321321`

Or use something like fmt.Sprintf to achieve the above, but making it easier to read.

b := fmt.Sprintf("cotent=id: %s\nQueue: %s\nRequestor: %s\nPriority: %s\nSubject: %s\nText: %s",
    "ticket/new",
    "General",
    "asd@asd.saaai",
    "1",
    "asdasdd test 4",
    "12321321321321321321321321321321321321",
);

Here’s an example of the above method: https://goplay.space/#y9XfY1bI4sI

You can read more about string formatting here: https://golang.org/pkg/fmt/ and specifically for fmt.Sprintf here: https://golang.org/pkg/fmt/#Sprintf

1 Like

Hi to @BB-8 and @NobbZ :slight_smile:

OK, I will past working and not working exp as I do not know what could be wrong. I have removed tabs (/t) but the result is the same. With curl is working, with go I can not get it working.

working:

not working:

What about transfer encoding: chunked?

pasting also how i do it with curl:
curl --data-urlencode $'content="id: ticket/new\nQueue: General\nRequestor: [asdasd@asdds.si\nPriority](mailto:asd@asdads.si\nPriority): 1\nSubject: dasfrom command line\nText: tekst vrstica 1\n tekst vrstica 2\n tekst vrstica 3"' 'http://172.31.1.193/rt/REST/1.0/ticket/new?user=root&pass=asdasdas'

thank you!

Hi @miha

I’m not quite sure what exactly it is you’re trying to achieve? Can you provide an example of what you’re expecting as the output and I can try and help?

Thanks!