JSON

authentication, authorization and use of raw headers

字号+ 作者:H5之家 来源:H5之家 2017-12-16 10:19 我要评论( )

三明京彩为广大玩家提供最新、最全、最具特色的三明蜘蛛资讯,同时还有各种八卦奇闻趣事。看蜘蛛资讯,就来三明京彩!

import akka.actor._ import akka.stream._ import akka.stream.scaladsl._ import akka.http.scaladsl.Http import scala.util._ import akka._ import akka.http.scaladsl.common._ import spray.json.DefaultJsonProtocol import akka.http.scaladsl.marshallers.sprayjson.SprayJsonSupport import akka.http.scaladsl.common.EntityStreamingSupport import akka.http.scaladsl.model._ import spray.json._ trait MyFormats extends SprayJsonSupport with DefaultJsonProtocol object Converters extends MyFormats { case class County(id: Int, name: String) implicit val countyFormat = jsonFormat2(County) } object HttpClientDemo extends App { import Converters._ ) implicit val mat = ActorMaterializer() implicit val ec = sys.dispatcher implicit val jsonStreamingSupport: JsonEntityStreamingSupport = EntityStreamingSupport.json() import akka.util.ByteString import akka.http.scaladsl.model.HttpEntity.limitableByteSource val source: Source[County,NotUsed] = Source()} def countyToByteString(c: County) = { ByteString(c.toJson.toString) } val flowCountyToByteString : Flow[County,ByteString,NotUsed] = Flow.fromFunction(countyToByteString) val rowBytes = limitableByteSource(source via flowCountyToByteString) import akka.http.scaladsl.model.headers._ val request = HttpRequest(HttpMethods.POST,uri = s) .addHeader(RawHeader(,)) .addCredentials(BasicHttpCredentials(, )) val data = HttpEntity( ContentTypes.`application/json`, rowBytes ) def uploadRows(request: HttpRequest, dataEntity: RequestEntity) = { val futResp = Http(sys).singleRequest( 枣庄京彩request.copy(entity = dataEntity) ) futResp .andThen { case Success(r@HttpResponse(StatusCodes.OK, _, entity, _)) => entity.dataBytes.map(_.utf8String).runForeach(println) case Success(r@HttpResponse(code, _, _, _)) => println(s) r.discardEntityBytes() ) ) } } uploadRows(request,data) s盐城京彩cala.io.StdIn.readLine() sys.terminate() }

 

1.本站遵循行业规范,任何转载的稿件都会明确标注作者和来源;2.本站的原创文章,请转载时务必注明文章作者和来源,不尊重原创的行为我们将追究责任;3.作者投稿可能会经我们编辑修改或补充。

相关文章
网友点评