tleyden / open-ocr

Run your own OCR-as-a-Service using Tesseract and Docker
Apache License 2.0
1.33k stars 223 forks source link

Does open-ocr accept multipart/form-data ? #75

Closed GeorgeAnanthSoosai closed 8 years ago

GeorgeAnanthSoosai commented 8 years ago

it will be easy for me to sent multipart/formdata..

Is this the only place to change this ?

func (s OcrHttpMultipartHandler) extractParts(req http.Request) (OcrRequest, error) {

logg.LogTo("OCR_HTTP", "request to ocr-file-upload")
ocrReq := OcrRequest{}

switch req.Method {
case "POST":
    h := req.Header.Get("Content-Type")
    logg.LogTo("OCR_HTTP", "content type: %v", h)

    contentType, attrs, _ := mime.ParseMediaType(req.Header.Get("Content-Type"))
    logg.LogTo("OCR_HTTP", "content type: %v", contentType)

    # **if !strings.HasPrefix(h, "multipart/related") { // can i change this to multipart/formdata. ?**
        return ocrReq, fmt.Errorf("Expected multipart related")
    }

    reader := multipart.NewReader(req.Body, attrs["boundary"])

    for {

        part, err := reader.NextPart()

        if err == io.EOF {
            break
        }
        contentTypeOuter := part.Header["Content-Type"][0]
        contentType, attrs, _ := mime.ParseMediaType(contentTypeOuter)

        logg.LogTo("OCR_HTTP", "attrs: %v", attrs)

        switch contentType {
        # **case "application/json":  // Can we remove this section and handcode the json string ?**
            decoder := json.NewDecoder(part)
            err := decoder.Decode(&ocrReq)
            if err != nil {
                return ocrReq, fmt.Errorf("Unable to unmarshal json: %s", err)
            }
            part.Close()
        default:
            if !strings.HasPrefix(contentType, "image") {
                return ocrReq, fmt.Errorf("Expected content-type: image/*")
            }

            partContents, err := ioutil.ReadAll(part)
            if err != nil {
                return ocrReq, fmt.Errorf("Failed to read mime part: %v", err)
            }

            ocrReq.ImgBytes = partContents
            return ocrReq, nil

        }

    }

    return ocrReq, fmt.Errorf("Didn't expect to get this far")

default:
    return ocrReq, fmt.Errorf("This endpoint only accepts POST requests")
}

}

tleyden commented 8 years ago

This doesn't look like an open-ocr bug. Please file bugs with:

GeorgeAnanthSoosai commented 8 years ago

Can we me the ocr call to success on Postman rest call ?

Up your docker server

and try hit the post call in the postman plugin... It should success the call here with 200...

Can you make that success for me ??