小编use*_*042的帖子

如何通过Swift发送POST请求?

我有这样的控制器 -

def create
   if (@user = User.find_by_email(params[:email])) && @user.valid_password?(params[:password])
      render json: @user.as_json(only: [:email,:authentication_token]),status: :created
   else 
      render json:('Unauthorized Access')
   end  
end 
Run Code Online (Sandbox Code Playgroud)

当我使用Postman发出此请求时,我选择Body,并形成数据并添加电子邮件和密码.这个工作

在此输入图像描述

如何使用swift做同样的事情?这就是我尝试过的

let url = URL(string: "http://localhost:3000/api/v1/user_serialized/")

let config = URLSessionConfiguration.default

let request = NSMutableURLRequest(url: url!)

request.httpMethod = "POST"

let bodyData = "email=Test@test.com&password=Test1234"

request.httpBody = bodyData.data(using: String.Encoding.utf8);

let session = URLSession(configuration: config)

let task = session.dataTask(with: url! as URL, completionHandler: {(data, response, error) in
    let json = JSON(data:data!)

    debugPrint(json)
})

task.resume()
Run Code Online (Sandbox Code Playgroud)

xcode json postman swift

10
推荐指数
2
解决办法
3万
查看次数

如果列表只包含0,我如何在python中打印?

如果列表只包含0s,我如何在python中打印?

list1=[0,0,0,0,0,0]
if list1 has all 0s
print("something")
Run Code Online (Sandbox Code Playgroud)

我希望输出是"东西"

python list

3
推荐指数
1
解决办法
152
查看次数

如何正确使用打印功能

while index < len(diecount):
    print(index)
    for number in range(diecount[index]):
        print('*')

    index+=1
    print("")
Run Code Online (Sandbox Code Playgroud)

目前我正在接受

1
**
2
3
**
Run Code Online (Sandbox Code Playgroud)

我希望输出

1 **
2
3 **
Run Code Online (Sandbox Code Playgroud)

python

2
推荐指数
1
解决办法
159
查看次数

我该如何修复这个python循环?

outcome=input("higher or lower")

while (outcome!= 'h' or outcome!= 'l'): 
    outcome=input("enter h or l")

print("its working")
Run Code Online (Sandbox Code Playgroud)

即使我写H或L,while循环仍然继续

python python-3.x

0
推荐指数
1
解决办法
64
查看次数

结束一个程序

如果事情属实,我如何结束我的计划?

这是我的代码

  count=0

  num=input("What would you like to do [1,2,3,4]? ")
  while (num>'0' and num<'5'):
      while num=='1':
        Do something
      while num=='2':
        Do something
      While num=='3':
        Do something
      while num=='4' and count!=1:

         print("The End")
         count= count+1
Run Code Online (Sandbox Code Playgroud)

我希望程序在num为'4'时结束

python python-3.x

0
推荐指数
1
解决办法
570
查看次数

标签 统计

python ×4

python-3.x ×2

json ×1

list ×1

postman ×1

swift ×1

xcode ×1