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

How to set http client post method #202

Closed
tianxiaoliang opened this issue Nov 16, 2016 · 9 comments
Closed

How to set http client post method #202

tianxiaoliang opened this issue Nov 16, 2016 · 9 comments

Comments

@tianxiaoliang
Copy link

In net/http,I can declare request and set method use below
// NewRequest returns a new Request given a method, URL, and optional body.
//
// If the provided body is also an io.Closer, the returned
// Request.Body is set to body and will be closed by the Client
// methods Do, Post, and PostForm, and Transport.RoundTrip.
//
// NewRequest returns a Request suitable for use with Client.Do or
// Transport.RoundTrip.
// To create a request for use with testing a Server Handler use either
// ReadRequest or manually update the Request fields. See the Request
// type's documentation for the difference between inbound and outbound
// request fields.
func NewRequest(method, urlStr string, body io.Reader) (*Request, error)

and then use Do method to send request

But How to set method like "PUT" and "DELETE" in fasthttp.Request

@ghost
Copy link

ghost commented Nov 16, 2016

@tianxiaoliang

You can easly .Do with fasthttp also,

    req := fasthttp.AcquireRequest()

    req.URI().Update("mysite.com/mypath")
    req.Header.SetMethodBytes([]byte("PUT")) // or "DELETE" 

    res := fasthttp.AcquireResponse()

    err := ClientFasthttp.Do(req, res)
        //... check res.XXX methods for the response from mysite.com/mypath


      // when finish 
      fasthttp.ReleaseRequest(req)
      fasthttp.ReleaseResponse(res)

Hope that helped you!

@tianxiaoliang
Copy link
Author

When I use GET in this code,I can not get result from server ,and it returns tomcat error
HTTP Status 400 - Bad Content-Type header value: 'Accept-Encoding'

type Status report

message Bad Content-Type header value: 'Accept-Encoding'

description The request sent by the client was syntactically incorrect.

The code is below
req := fasthttp.AcquireRequest()

req.URI().Update("mysite.com/mypath")
req.Header.SetMethodBytes([]byte("GET")) 

res := fasthttp.AcquireResponse()

err := fasthttp.Client.Do(req, res)
    //... check res.XXX methods for the response from mysite.com/mypath


  // when finish 
  fasthttp.ReleaseRequest(req)
  fasthttp.ReleaseResponse(res)

@tianxiaoliang
Copy link
Author

It is because I add
req.Header.Add("Content-Type", "application/json")
req.Header.Add("Accept", "application/json")
and then I got the 400 error from tomcat

Why

@tianxiaoliang
Copy link
Author

GET http://127.0.0.1:8080/eureka/v2/apps/629d2b13-b144-4b4d-ba83-292b0d9a5a48 HTTP/1.1
User-Agent: fasthttp
Content-Type: application/json
Accept: application/json

Seems everything is fine ,If I don't set header,the request will be success,and the default Accept is xml,But I want Json response.I can get json response by net/http,But after I replace with fasthttp,it report 400 error

@tianxiaoliang
Copy link
Author

I can get json response by below code

package main

import (
"fmt"
"net/http"
"io/ioutil"
)

func main() {

url := "http://127.0.0.1:8080/eureka/v2/apps/a9bf404e-391c-435d-9cce-15f73558679f"

req, _ := http.NewRequest("GET", url, nil)

req.Header.Add("content-type", "application/json")
req.Header.Add("accept", "application/json")

res, _ := http.DefaultClient.Do(req)

defer res.Body.Close()
body, _ := ioutil.ReadAll(res.Body)

fmt.Println(res)
fmt.Println(string(body))

}

But I have to remove req.Header.Add("content-type", "application/json") in fasthttp to get right json response or it will report 400 status
below is fasthttp version:

package main

import (
"fmt"
"github.com/valyala/fasthttp"
)

func main() {
req :=NewRequest("GET","http://127.0.0.1:8080/eureka/v2/apps/a9bf404e-391c-435d-9cce-15f73558679f",nil)
req.Header.Set("Accept", "application/json")
fmt.Println(req.Header.String())
res := fasthttp.AcquireResponse()
client :=&fasthttp.Client{}
_ = client.Do(req,res)

fmt.Println(req.URI())
fmt.Println(res.StatusCode())
fmt.Println(string(res.Body()))
fmt.Print(string(req.Header.Peek("Accept-Encoding")))
fasthttp.ReleaseRequest(req)
fasthttp.ReleaseResponse(res)

}
func NewRequest(method string, reqURL string, body []byte) *fasthttp.Request {
req := fasthttp.AcquireRequest()
req.Header.SetMethodBytes([]byte(method))
req.SetBody(body)
req.SetRequestURI(reqURL)

return req

}

@tianxiaoliang
Copy link
Author

Use net/http I got Json in right characters
But use fasthttp ,I got unreadable characters,How can I covert them into string
fmt.Println(bytes.NewBuffer(res.Body()).String())
䃧"�N�_E�{�K�p�ؖ�Ћ_���I)��t}��ώw���wƥu +d����eͦLlw]��0N�<�#6d{e�
8���� 3�����O� �oH�h��w�x�gI4�
`�s�����wi�O�d�����4�������2{�&�@���PW�����q�̕�xJm��F�����D�aT�?�r��^5����F��wNOG��/�F=�4����<T�A��|����O��ʓ���,��D2�+�MW����~�s�L�%�E�
n+��nCR�9���J�I8����]��Vp��R���l�fI��U?�N{%gE�(�����qsi��
fmt.Println(fmt.Sprintf("%v", res.Body()))
[31 139 8 0 0 0 0 0 0 0 141 83 93 111 226 48 16 252 47 22 143 40 36 13 228 131 167 34 130 78 168 42 69 229 162 123 222 75 22 112 235 216 150 237 208 139 42 254 251 173 73 41 7 170 116 125 176 18 207 142 119 189 179 227 119 198 165 117 32 43 100 211 247 207 255 101 205 166 76 108 119 93 228 194 48 78 227 60 207 35 54 100 123 101 221 10 26 252 50 8 90 19 190 40 159 23 15 51 218 114 61 171 107 67 72 20 6 81 76 107 146 209 202 41 66 37 92 107 41 82 174 105 167 14 104 12 175 107 148 23 124 245 176 122 250 181 162 160 86 198 249 123 13 216 52 11 179 112 200 238 81 194 111 129 254 122 206 180 200 142 148 14 171 214 224 250 194 28 143 227 43 226 22 132 61 49 43 213 74 103 58 223 92 52 100 53 56 152 163 116 104 150 114 171 252 217 251 74 128 245 23 168 84 19 72 116 91 193 255 4 212 22 167 120 176 60 75 67 155 65 129 91 104 133 43 174 83 12 153 236 197 121 236 158 222 164 175 40 16 44 158 211 27 148 248 6 98 233 249 7 255 221 96 69 213 98 106 171 110 13 56 174 228 25 203 9 51 184 227 214 245 248 79 222 32 149 111 72 225 104 156 230 119 217 120 18 103 73 52 161 10 96 221 115 159 248 134 20 167 119 105 22 79 242 100 200 240 192 171 155 52 161 23 206 16 142 165 254 50 123 152 38 212 64 131 14 188 80 87 242 188 192 1 130 214 113 17 204 149 16 120 74 109 7 139 70 187 238 17 180 239 155 68 251 97 84 171 63 204 114 25 232 94 53 184 134 29 150 70 16 190 119 78 79 71 163 27 47 157 70 61 250 52 202 247 232 155 222 60 84 1 65 184 253 124 143 213 235 255 79 245 228 202 147 233 232 161 247 44 158 154 68 50 213 43 4 77 87 171 6 184 244 126 240 174 182 115 165 76 205 37 141 69 238 10 110 43 239 223 110 67 82 162 57 155 178 159 74 169 73 56 172 255 17 151 93 205 238 131 86 112 227 186 27 82 26 142 147 108 18 102 73 226 223 85 63 186 78 123 37 103 69 177 40 216 241 248 23 164 113 115 105 183 3 0 0]

@tianxiaoliang
Copy link
Author

I found response header "Content-Encoding: gzip" So I unzip the body,the result is fine,But still have a problem Why I set header Content-type to application/json, tomcat report 400,but net/http is fine

@tianxiaoliang
Copy link
Author

I print out the whole Request,the content-type is application/json,not Accept-Encoding

GET /eureka/v2/apps/EUREKA HTTP/1.1
User-Agent: fasthttp
Host: 10.162.197.95:30201
Content-Type: application/json;
Accept: application/json;

@tianxiaoliang
Copy link
Author

Seems both Content-Type and Accept,doesn't take affect to API /eureka/v2/apps/EUREKA
But take affect to API /eureka/v2/instances/{instanceid}

But net/http doesn't have this issue to call eureka

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

2 participants