2013-06-12 8 views
6

스프레이를 사용하여 멀티 파트 폼을 서버에 게시하려고합니다. 특히 이미지를 게시하고 싶습니다.스프레이 클라이언트 포스트 멀티 팩

파일을 멀티 마샬링하는 것이 무슨 문제입니까? 스프레이에서 기본 Marshaller에 대해 언급 했음에도 불구하고 둘 중 하나를 함께 연결하는 것만 같습니다.

현재 스프레이 1.0-M7을 사용 중인데 현재 스칼라 2.10으로 마이그레이션하지 않았으므로 예제가 해당 분기와 호환 될 수 있다면 멋질 것입니다.

는 내가 현재 가지고하는 것은 이것이다 :

val bis = new BufferedInputStream(new FileInputStream(file)) 
val bArray = Stream.continually(bis.read).takeWhile(-1 !=).map(_.toByte).toArray 
Logger.error("About to post with spray") 
pipeline(Post("/saveImageWithSpray", bArray)) 

물론 나는 오류 말하는 얻을 :

For request 'POST /saveImageWithSpray' [Missing boundary header] 

대부분의 예는 내가 마샬에 대한 지침 ([X] 등) 콘텐츠를 사용 찾기 , 아직 스프레이 라우팅을 사용하고 있지는 않습니다. 다른 프레임 워크에 구축 된 응용 프로그램에서 스프레이 클라이언트를 사용하여 게시물을 실행하기 만하면됩니다.

감사

편집

내가 실제로 이런 식으로 마샬로 처리했다 :

val pipeline = (
addHeader("Content-Type", "multipart/form-data") 
    ~> sendReceive(conduit) 
) 
val bis = new BufferedInputStream(new FileInputStream(file, "UTF-8")) 
val bArray = Stream.continually(bis.read).takeWhile(-1 !=).map(_.toByte).toArray 

Logger.error("About to post with spray "+bArray.length.toString) 
pipeline(Post("/saveImageWithSpray", MultipartFormData(Map(
    "spray-file" -> BodyPart(
    HttpEntity(Some(HttpBody(ContentType(MediaTypes.`image/gif`), bArray))), 
    HttpHeaders.`Content-Disposition`("form-data", Map("name" -> "spray-file","filename"->"Fuurin (Glass Wind Chime).gif"))::Nil 
    ) 
)))) 

불행하게도이 여전히 작동하지 않는, 데이터 전송지고 있지만 서버는 찾을 수 없습니다 파일.

POST /saveImageWithSpray HTTP/1.1 
Host: localhost:9000 
User-Agent: spray-can/1.0-M7 
Content-Type: multipart/form-data; boundary="oxz40rxXXQyDx+IUKcz7QYpJ" 
Content-Length: 1725 

--oxz40rxXXQyDx+IUKcz7QYpJ 
Content-Disposition: form-data; name="spray-file" 
Content-Disposition: form-data; name="spray-file"; filename="Fuurin (Glass Wind Chime).gif" 
Content-Type: image/gif 

GIF89a0.0.......... 
BINARY DATA 
..P...L0..8.....X.....l..?...; 
--oxz40rxXXQyDx+IUKcz7QYpJ--HTTP/1.1 500 Internal Server Error 
Content-Type: text/plain; charset=utf-8 
Content-Length: 25 

File not found spray-file 

이는 고급 나머지 클라이언트로 만든 유효한 요청의 캡처입니다 : 응답에 대한

POST /saveImageWithSpray HTTP/1.1 
Host: localhost:9000 
Connection: keep-alive 
Content-Length: 2573 
User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.110 Safari/537.36 
Origin: chrome-extension://hgmloofddffdnphfgcellkdfbfbjeloo 
Content-Type: multipart/form-data; boundary=----WebKitFormBoundaryuiIgwVg3rBQLFNGB 
Accept: */* 
Accept-Encoding: gzip,deflate,sdch 
Accept-Language: en-US,en;q=0.8 

------WebKitFormBoundaryuiIgwVg3rBQLFNGB 
Content-Disposition: form-data; name="spray-file"; filename="Gunsen (Fan) .gif" 
Content-Type: image/gif 

GIF89a0.0.........u.QQ..Z..z.wW[[[. 
BINARY DATA 
.....&...Z(.c.Q.....T.B7..S...!...p[...8."...; 
------WebKitFormBoundaryuiIgwVg3rBQLFNGB-- 
HTTP/1.1 200 OK 
Content-Type: text/plain; charset=utf-8 
Content-Length: 24 

File uploaded with spray 
+2

'filename' 매개 변수를 지원하지 않는 현재 MultipartFormData 마샬 러가 부족한 것 같습니다. 이 문제를 추적하기 위해 https://github.com/spray/spray/issues/327을 작성했습니다. – jrudolph

+0

그래서 @dgrandes ... 수정 작업을 했습니까? 양식 데이터 요소의 스프레이 파일 이름입니까? 텍스트/csv 파일 및 인증 문자열과 같은 두 가지 양식 데이터 요소와 비슷한 문제가 있습니다. 직접 경계 문자열을 만들었습니까? 감사합니다 – iyerland

+0

죄송합니다 @iyerland, 나는 스프레이 솔루션을 포기하고 플레이에서 직접 구현! 뼈대. 죄송합니다! – dgrandes

답변

1

@grandes 감사

Wireshark를 캡처는 다음 보여준다. newman rest 클라이언트 라이브러리 (https://github.com/stackmob/newman)를 사용하여 HTTP 요청을 보내지 만 수동으로 MultipartFormData를 만들어야했습니다. 가까운 시일 내에 뉴먼에서 고칠 것입니다. 다음은 다중 파트 양식 데이터를 수동으로 작성하는 데 사용한 링크입니다. http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4.2