简体   繁体   中英

cURL works but none of the rest clients with Go sebserver

Having a simple GO WebServer which accepts an image as part of POST request.

Code snippet - Request is mapped to this function

 func UploadFile(w http.ResponseWriter, r *http.Request, p httprouter.Params) {
        successResponse := models.HTTPResponse {
            FileURL:"http://testing.com",
        }
        WrapResponse(w, successResponse, http.StatusOK)
    }

Response writer function

func WrapResponse(writer http.ResponseWriter, content interface{}, status int) {    
    writer.Header().Set("Content-Type", "application/json")
    writer.WriteHeader(status)
    // Content is a struct Response { fileURL string }  
    responseJson, err := json.Marshal(content)  
    CheckError(err, "Error wrapping response")  
    writer.Write(responseJson) 
    }

    func CheckError(err error, msg string) {    
    if err != nil {         
    panic(fmt.Sprintf("%s : %s", msg, err))     
    } 
    }

When i hit the URL using cURL as below the response is 200 OK (as expected)

curl -X POST -d@ "Screen Shot 2015-11-15 at 6.09.58 pm.png" http://localhost:8000/image/agent123/property --header "Content-Type:image/png" --header "X-User-Agent:agent-php" response -->{"fileURL":" http://testing.com "}%

Entire cURL request & response冗长的

But when i try the same from DHC rest client, have also tried with advanced rest client getting no response.

通过 DHC 的相同请求

Edit 1: Request does reach the server when fired from rest clients

I do not believe you're doing what you think you are.

curl -X POST -d "Screen Shot 2015-11-15 at 6.09.58 pm.png" http://localhost:8000/image/agent123/property --header "Content-Type:image/png" --header "X-User-Agent:agent-php" 

Will not send the file "Screen Shot 2015-11-15 at 6.09.58 pm.png", it's gonig to send the literal text as the body. You probably want "-d @'Screen Shot 2015-11-15 at 6.09.58 pm.png'".

Which then your "good" result is not exactly good, so you have to figure out what the bug is at the receiving end on the server. As the GUI client you're using is probably sending the file, whereas cURL is not. And your backend seems to accept text, not files.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM